Subversion Repositories Projects

Rev

Rev 2236 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2236 Rev 2250
Line 48... Line 48...
48
 
48
 
49
namespace MKLiveView
49
namespace MKLiveView
50
{
50
{
51
    public partial class MainForm : Form
51
    public partial class MainForm : Form
-
 
52
    {
-
 
53
        String[] NC_Error = new string[44]
-
 
54
        {
-
 
55
            "No Error",
-
 
56
            "FC not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A1_.22FC_not_compatible_.22",
-
 
57
            "MK3Mag not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A2_.22MK3Mag_not_compatible_.22",
-
 
58
            "no FC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A3_.22no_FC_communication_.22",
-
 
59
            "no compass communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A4_.22no_compass_communication_.22",
-
 
60
            "no GPS communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A5_.22no_GPS_communication_.22",
-
 
61
            "bad compass value" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A6_.22bad_compass_value.22",
-
 
62
            "RC Signal lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A7_.22RC_Signal_lost_.22",
-
 
63
            "FC spi rx error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A8_.22FC_spi_rx_error_.22",
-
 
64
            "ERR: no NC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A9:_.22ERR:_no_NC_communication.22",
-
 
65
            "ERR: FC Nick Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A10_.22ERR:_FC_Nick_Gyro.22",
-
 
66
            "ERR: FC Roll Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A11_.22ERR:_FC_Roll_Gyro.22",
-
 
67
            "ERR: FC Yaw Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A12_.22ERR:_FC_Yaw_Gyro.22",
-
 
68
            "ERR: FC Nick ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A13_.22ERR:_FC_Nick_ACC.22",
-
 
69
            "ERR: FC Roll ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A14_.22ERR:_FC_Roll_ACC.22",
-
 
70
            "ERR: FC Z-ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A15_.22ERR:_FC_Z-ACC.22",
-
 
71
            "ERR: Pressure sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A16_.22ERR:_Pressure_sensor.22",
-
 
72
            "ERR: FC I2C" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A17_.22ERR:_FC_I2C.22",
-
 
73
            "ERR: Bl Missing" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A18_.22ERR:_Bl_Missing.22",
-
 
74
            "Mixer Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A19_.22Mixer_Error.22",
-
 
75
            "FC: Carefree Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A20_.22FC:_Carefree_Error.22",
-
 
76
            "ERR: GPS lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A21_.22ERR:_GPS_lost.22",
-
 
77
            "ERR: Magnet Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A22_.22ERR:_Magnet_Error.22",
-
 
78
            "Motor restart" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A23_.22Motor_restart.22",
-
 
79
            "BL Limitation" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A24_.22BL_Limitation.22",
-
 
80
            "Waypoint range" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A25_.22Waypoint_range.22",
-
 
81
            "ERR:No SD-Card" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A26_.22ERR:No_SD-Card.22",
-
 
82
            "ERR:SD Logging aborted" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A27_.22ERR:SD_Logging_aborted.22",
-
 
83
            "ERR:Flying range!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A28_.22ERR:Flying_range.21.22",
-
 
84
            "ERR:Max Altitude" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A29_.22ERR:Max_Altitude.22",
-
 
85
            "No GPS Fix" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A30_.22No_GPS_Fix.22",
-
 
86
            "compass not calibrated" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A31_.22compass_not_calibrated.22",
-
 
87
            "ERR:BL selftest" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A32_.22ERR:BL_selftest.22",
-
 
88
            "no ext. compass" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A33_.22no_ext._compass.22",
-
 
89
            "compass sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A34_.22compass_sensor.22",
-
 
90
            "FAILSAFE pos.!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A35_.22FAILSAFE_pos..21__.22",
-
 
91
            "ERR:Redundancy" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A36_.22ERR:Redundancy__.22",
-
 
92
            "Redundancy test" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A37_.22Redundancy_test_.22",
-
 
93
            "GPS Update rate" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A38_.22GPS_Update_rate.22",
-
 
94
            "ERR:Canbus" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A39_.22ERR:Canbus.22",
-
 
95
            "ERR: 5V RC-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A40_.22ERR:_5V_RC-Supply.22",
-
 
96
            "ERR:Power-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A41_.22ERR:Power-Supply.22",
-
 
97
            "ACC not calibr." + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A42_.22ACC_not_calibr..22",
-
 
98
            "ERR:Parachute!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A43_.22ERR:Parachute.21.22"
Line 52... Line 99...
52
    {
99
        };
53
 
100
 
54
        [FlagsAttribute]
101
        [FlagsAttribute]
55
        enum NC_HWError0 : short
102
        enum NC_HWError0 : short
Line 128... Line 175...
128
        byte OSDInterval = 85;
175
        byte OSDInterval = 85;
129
        /// <summary>
176
        /// <summary>
130
        /// datatable for the debug data array - displayed on settings tabpage in datagridview
177
        /// datatable for the debug data array - displayed on settings tabpage in datagridview
131
        /// </summary>
178
        /// </summary>
132
        DataTable dtAnalog = new DataTable();
179
        DataTable dtAnalog = new DataTable();
-
 
180
 
-
 
181
        DataTable dtMotors1 = new DataTable();
-
 
182
        DataTable dtMotors2 = new DataTable();
-
 
183
 
133
        public MainForm()
184
        public MainForm()
134
        {
185
        {
135
            InitializeComponent();
186
            InitializeComponent();
136
            _readIni();
187
            _readIni();
137
            dtAnalog.Columns.Add("ID");
188
            dtAnalog.Columns.Add("ID");
138
            dtAnalog.Columns.Add("Value");
189
            dtAnalog.Columns.Add("Value");
139
            dataGridView1.DataSource = dtAnalog;
190
            dataGridView1.DataSource = dtAnalog;
-
 
191
            dtMotors1.Columns.Add("#");
-
 
192
            dtMotors1.Columns.Add("Current");
-
 
193
            dtMotors1.Columns.Add("Temp");
-
 
194
            dtMotors2.Columns.Add("#");
-
 
195
            dtMotors2.Columns.Add("Current");
-
 
196
            dtMotors2.Columns.Add("Temp");
-
 
197
            dgvMotors1.DataSource = dtMotors1;
-
 
198
            dgvMotors2.DataSource = dtMotors2;
-
 
199
            _initDTMotors();
-
 
200
            dgvMotors1.Columns[0].Width = 24;
-
 
201
            dgvMotors1.Columns[1].Width = 74;
-
 
202
            dgvMotors1.Columns[2].Width = 74;
-
 
203
            dgvMotors2.Columns[0].Width = 24;
-
 
204
            dgvMotors2.Columns[1].Width = 74;
-
 
205
            dgvMotors2.Columns[2].Width = 74;
140
            simpleSerialPort.PortClosed += SimpleSerialPort_PortClosed;
206
            simpleSerialPort.PortClosed += SimpleSerialPort_PortClosed;
141
            simpleSerialPort.PortOpened += SimpleSerialPort_PortOpened;
207
            simpleSerialPort.PortOpened += SimpleSerialPort_PortOpened;
142
            simpleSerialPort.DataReceived += processMessage;
208
            simpleSerialPort.DataReceived += processMessage;
143
            chkbAutoBL.Checked = _blctrlDataAutorefresh;
209
            chkbAutoBL.Checked = _blctrlDataAutorefresh;
144
            chkbAutoDbg.Checked = _debugDataAutorefresh;
210
            chkbAutoDbg.Checked = _debugDataAutorefresh;
Line 153... Line 219...
153
        #region events
219
        #region events
154
        private void MainForm_Shown(object sender, EventArgs e)
220
        private void MainForm_Shown(object sender, EventArgs e)
155
        {
221
        {
156
            _loadLabelNames();
222
            _loadLabelNames();
157
            _init = false;
223
            _init = false;
-
 
224
            splitContainer1.SplitterDistance = 514;
158
        }
225
        }
159
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
226
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
160
        {
227
        {
161
            _writeIni();
228
            _writeIni();
162
        }
229
        }
Line 183... Line 250...
183
        {
250
        {
184
            if(bReadContinously)
251
            if(bReadContinously)
185
            {
252
            {
186
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
253
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
Line 187... Line 254...
187
 
254
 
Line 188... Line 255...
188
                if (_blctrlDataAutorefresh && _iCtrlAct == 2) { _readBLCtrl(true); Thread.Sleep(10); }
255
                if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); }
189
               
256
               
190
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
257
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
191
                check_HWError = true;
258
                check_HWError = true;
Line 207... Line 274...
207
        private void cbOSD_SelectedIndexChanged(object sender, EventArgs e)
274
        private void cbOSD_SelectedIndexChanged(object sender, EventArgs e)
208
        {
275
        {
209
            if (!_bCBInit && cbOSD.SelectedIndex > -1)
276
            if (!_bCBInit && cbOSD.SelectedIndex > -1)
210
                _OSDMenue(cbOSD.SelectedIndex);
277
                _OSDMenue(cbOSD.SelectedIndex);
211
        }
278
        }
212
 
-
 
213
        private void chkbAutoDbg_CheckedChanged(object sender, EventArgs e)
279
        private void chkbAutoDbg_CheckedChanged(object sender, EventArgs e)
214
        {
280
        {
215
            if(!_init) _debugDataAutorefresh = chkbAutoDbg.Checked;
281
            if(!_init) _debugDataAutorefresh = chkbAutoDbg.Checked;
216
        }
282
        }
217
        private void chkbAutoNav_CheckedChanged(object sender, EventArgs e)
283
        private void chkbAutoNav_CheckedChanged(object sender, EventArgs e)
Line 224... Line 290...
224
        }
290
        }
225
        private void chkbAutoOSD_CheckedChanged(object sender, EventArgs e)
291
        private void chkbAutoOSD_CheckedChanged(object sender, EventArgs e)
226
        {
292
        {
227
            if (!_init) _OSDAutorefresh = chkbAutoOSD.Checked;
293
            if (!_init) _OSDAutorefresh = chkbAutoOSD.Checked;
228
        }
294
        }
229
 
-
 
230
        private void cbTimingDebug_SelectedIndexChanged(object sender, EventArgs e)
295
        private void cbTimingDebug_SelectedIndexChanged(object sender, EventArgs e)
231
        {
296
        {
232
            if (cbTimingDebug.SelectedIndex > -1)
297
            if (cbTimingDebug.SelectedIndex > -1)
233
            {
298
            {
234
                debugInterval = (byte)(Convert.ToInt16(cbTimingDebug.SelectedItem) / 10);
299
                debugInterval = (byte)(Convert.ToInt16(cbTimingDebug.SelectedItem) / 10);
Line 315... Line 380...
315
            }));
380
            }));
316
        }
381
        }
Line 317... Line 382...
317
 
382
 
Line -... Line 383...
-
 
383
        #region functions        
318
        #region functions        
384
 
-
 
385
        #region processing received data
-
 
386
        /// <summary> Processing the messages and displaying them in the according form controls 
319
 
387
        /// function called by simpleSerialPort.DataReceived event
320
        /// <summary> Processing the messages and displaying them in the according form controls </summary>
388
        /// </summary>
321
        /// <param name="message"> message bytearray recieved by SimpleSerialPort class </param>
389
        /// <param name="message"> message bytearray recieved by SimpleSerialPort class </param>
322
        private void processMessage(byte[] message)
390
        private void processMessage(byte[] message)
323
        {
391
        {
Line 338... Line 406...
338
                    FlightControllerMessage.ParseMessage(message, out cmdID, out adr, out data);
406
                    FlightControllerMessage.ParseMessage(message, out cmdID, out adr, out data);
Line 339... Line 407...
339
 
407
 
340
                    if (adr == 255) { crcError++; }
408
                    if (adr == 255) { crcError++; }
341
                    else crcError = 0;
409
                    else crcError = 0;
342
                    lblCRCErr.Invoke((Action)(() => lblCRCErr.Text = crcError.ToString()));
-
 
-
 
410
                    lblCRCErr.Invoke((Action)(() => lblCRCErr.Text = crcError.ToString()));
343
 
411
                    //display the active controller (FC / NC) 
344
                    if (adr > 0 && adr < 3 && adr != _iCtrlAct) //adr < 3: temporary workaround cause when I've connected the FC alone it always switches between mk3mag & FC every second...???
412
                    if (adr > 0 && adr < 3 && adr != _iCtrlAct) //adr < 3: temporary workaround cause when I've connected the FC alone it always switches between mk3mag & FC every second...???
345
                    {
413
                    {
346
                        _iCtrlAct = adr;
414
                        _iCtrlAct = adr;
347
                        switch (adr)
415
                        switch (adr)
Line 375... Line 443...
375
                        s = new string(ASCIIEncoding.ASCII.GetChars(data, 1, data.Length - 1));
443
                        s = new string(ASCIIEncoding.ASCII.GetChars(data, 1, data.Length - 1));
376
                        s = s.Trim('\0', '\n');
444
                        s = s.Trim('\0', '\n');
Line 377... Line 445...
377
 
445
 
378
                        switch (cmdID)
446
                        switch (cmdID)
-
 
447
                        {
-
 
448
                            case 'A': //Label names
-
 
449
                                _processLabelNames(s);
-
 
450
                                break;
-
 
451
 
-
 
452
                            case 'D': //Debug data
-
 
453
                                _processDebugVals(adr,data);
-
 
454
                                break;
-
 
455
 
-
 
456
                            case 'V': //Version
-
 
457
                                _processVersion(adr, data);
-
 
458
                                break;
-
 
459
 
-
 
460
                            case 'K'://BL-CTRL data
-
 
461
                                _processBLCtrl(data);
-
 
462
                                break;
-
 
463
 
-
 
464
                            case 'O': //NC Data
-
 
465
                                _processNCData(data);
-
 
466
                                break;
-
 
467
 
-
 
468
                            case 'E': //NC error-string
-
 
469
                                ErrorLog(LogMsgType.Error, "NC Error: " + s);
-
 
470
                                break;
-
 
471
 
-
 
472
                            case 'L': //OSD Menue (called by pagenumber)
-
 
473
                                _processOSDSingle(data);
-
 
474
                                break;
-
 
475
 
-
 
476
                            case 'H': //OSD Menue (with autoupdate - called by Key)
-
 
477
                                _processOSDAuto(data);
-
 
478
                                break;
379
                        {
479
 
-
 
480
                            //default:
-
 
481
                            //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
-
 
482
                            //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
-
 
483
                            //    break;
-
 
484
                        }
-
 
485
                    }
-
 
486
                    //else
-
 
487
                    //{
-
 
488
                    //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
-
 
489
                    //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
-
 
490
                    //}
-
 
491
                }
-
 
492
            }
-
 
493
        }
-
 
494
        /// <summary>
-
 
495
        /// Analog label names 'A'
-
 
496
        /// each label name is returned as a single string 
-
 
497
        /// and added to string array sAnalogLabel[]
-
 
498
        /// and the datatable dtAnalog
-
 
499
        /// </summary>
-
 
500
        /// <param name="s">the label name</param>
-
 
501
        void _processLabelNames(string s)
380
                            case 'A':
502
        {
381
                                if (iLableIndex < 32)
503
            if (iLableIndex < 32)
382
                                {
504
            {
383
                                    sAnalogLabel[iLableIndex] = s;
505
                sAnalogLabel[iLableIndex] = s;
384
                                    if (dtAnalog.Rows.Count < 32)
506
                if (dtAnalog.Rows.Count < 32)
Line 387... Line 509...
387
                                        dtAnalog.Rows[iLableIndex].SetField(0, s);
509
                    dtAnalog.Rows[iLableIndex].SetField(0, s);
Line 388... Line 510...
388
 
510
 
389
                                    _getAnalogLabels(iLableIndex + 1);
511
                _getAnalogLabels(iLableIndex + 1);
390
                                }
512
            }
391
                                Debug.Print(s);
513
            Debug.Print(s);
392
 
514
        }
393
                                break;
515
        /// <summary>
-
 
516
        /// Debug values 'D'
-
 
517
        /// </summary>
-
 
518
        /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param>
-
 
519
        /// <param name="data">the received byte array to process</param>
-
 
520
        void _processDebugVals(byte adr,byte[] data)
394
                            case 'D':
521
        {
395
                                if (data.Length == 66)
522
            if (data.Length == 66)
396
                                {
523
            {
Line 397... Line 524...
397
                                    int[] iAnalogData = new int[32];
524
                int[] iAnalogData = new int[32];
Line 448... Line 575...
448
                                                    dTemp = Math.Pow((double)iAnalogData[index],2) + Math.Pow((double)iAnalogData[index - 1],2);
575
                                dTemp = Math.Pow((double)iAnalogData[index], 2) + Math.Pow((double)iAnalogData[index - 1], 2);
449
                                                    dTemp = Math.Sqrt(dTemp)/ (double)10; //'flat' distance from HP with N/E
576
                                dTemp = Math.Sqrt(dTemp) / (double)10; //'flat' distance from HP with N/E
450
                                                  //  lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0.00")));
577
                                                                       //  lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0.00")));
451
                                                    dTemp = Math.Pow(dTemp, 2) + Math.Pow(((double)iAnalogData[4] / (double)10), 2); //adding 'height' into calculation
578
                                dTemp = Math.Pow(dTemp, 2) + Math.Pow(((double)iAnalogData[4] / (double)10), 2); //adding 'height' into calculation
452
                                                    dTemp = Math.Sqrt(dTemp);
579
                                dTemp = Math.Sqrt(dTemp);
453
                                                    lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0 m")));
580
                                lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = dTemp.ToString("0 m")));
454
                                                    break;
581
                                break;
455
                                                case 31: //Sats used
582
                            case 31: //Sats used
456
                                                    lblNCSat.Invoke((Action)(() => lblNCSat.Text = sAnalogData[index]));
583
                                lblNCSat.Invoke((Action)(() => lblNCSat.Text = sAnalogData[index]));
457
                                                    break;
584
                                break;
458
                                            }
585
                        }
Line 499... Line 626...
499
                                        index++;
626
                    index++;
500
                                    }
627
                }
501
                                }
628
            }
502
                                else
629
            else
503
                                    Debug.Print("wrong data-length (66): " + data.Length.ToString());
630
                Debug.Print("wrong data-length (66): " + data.Length.ToString());
-
 
631
        }
504
                                break;
632
        /// <summary>
505
 
-
 
506
                            case 'V':
633
        /// Version string 'V'
-
 
634
        /// </summary>
-
 
635
        /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param>
-
 
636
        /// <param name="data">the received byte array to process</param>
-
 
637
        void _processVersion(byte adr,byte[] data)
-
 
638
        {
507
                                if (data.Length == 12)
639
            if (data.Length == 12)
508
                                {
640
            {
509
                                    if (!check_HWError)
641
                if (!check_HWError)
510
                                    {
642
                {
511
                                        string[] sVersionStruct = new string[10] { "SWMajor: ", "SWMinor: ", "ProtoMajor: ", "LabelTextCRC: ", "SWPatch: ", "HardwareError 1: ", "HardwareError 2: ", "HWMajor: ", "BL_Firmware: ", "Flags: " };
643
                    string[] sVersionStruct = new string[10] { "SWMajor: ", "SWMinor: ", "ProtoMajor: ", "LabelTextCRC: ", "SWPatch: ", "HardwareError 1: ", "HardwareError 2: ", "HWMajor: ", "BL_Firmware: ", "Flags: " };
Line 533... Line 665...
533
                                            ErrorLog(LogMsgType.Error, "NC - Unknown HW-ERROR: " + data[6].ToString()); //@moment NC has only one error field
665
                        ErrorLog(LogMsgType.Error, "NC - Unknown HW-ERROR: " + data[6].ToString()); //@moment NC has only one error field
534
                                    }
666
                }
Line 535... Line 667...
535
 
667
 
536
                                }
668
            }
-
 
669
            check_HWError = false;
-
 
670
        }
-
 
671
        /// <summary>
-
 
672
        /// BL-Ctrl data 'K'
-
 
673
        /// for FC you have to use a customized firmware
-
 
674
        /// </summary>
-
 
675
        /// <param name="data">the received byte array to process</param>
-
 
676
        void _processBLCtrl(byte[] data)
-
 
677
        {
-
 
678
            if (data.Length % 6 == 0) //data.Length up to 96 (16 motors x 6 byte data) --> new datastruct in FC -> not standard!
-
 
679
            {
-
 
680
                bool bAvailable = false;
-
 
681
                for (int i = 0; i < data.Length && data[i] < 8; i += 6) // data[i] < 8 --> at moment there are 8 display fields for motors
-
 
682
                {
-
 
683
 
537
                                check_HWError = false;
684
                    if ((data[i + 4] & 128) == 128) //Status bit at pos 7 = 128 dec -- if true, motor is available
-
 
685
                        bAvailable = true;
-
 
686
                    else
Line 538... Line -...
538
                                break;
-
 
539
 
687
                        bAvailable = false;
540
                            case 'K'://BL-CTRL debug data from NC
688
 
541
                                if (data.Length == 6 && data[0] < 8)
-
 
542
                                {
689
                    if (data[i] < 4)
543
                                    Label lbCur = new Label(), lbTemp = new Label();
690
                    {
544
                                    switch (data[0])
-
 
545
                                    {
691
                        if (bAvailable)
546
                                        case 0:
692
                        {
547
                                            lbCur = LBLNCM1Cur;
693
                            dtMotors1.Rows[data[i]].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A"));
548
                                            lbTemp = LBLNCM1Temp;
694
                            dtMotors1.Rows[data[i]].SetField(2, data[i + 2].ToString("0 °C"));
549
                                            break;
-
 
550
                                        case 1:
-
 
551
                                            lbCur = LBLNCM2Cur;
-
 
552
                                            lbTemp = LBLNCM2Temp;
695
                        }
553
                                            break;
696
                        else
554
                                        case 2:
697
                        {
555
                                            lbCur = LBLNCM3Cur;
698
                            dtMotors1.Rows[data[i]].SetField(1, "NA");
556
                                            lbTemp = LBLNCM3Temp;
699
                            dtMotors1.Rows[data[i]].SetField(2, "NA");
557
                                            break;
-
 
558
                                        case 3:
-
 
559
                                            lbCur = LBLNCM4Cur;
700
                        }
560
                                            lbTemp = LBLNCM4Temp;
701
                    }
561
                                            break;
-
 
562
                                        case 4:
-
 
563
                                            lbCur = LBLNCM5Cur;
702
                    if (data[i] > 3 && data[i] < 8)
564
                                            lbTemp = LBLNCM5Temp;
703
                    {
565
                                            break;
704
                        if (bAvailable)
566
                                        case 5:
705
                        {
567
                                            lbCur = LBLNCM6Cur;
706
                            dtMotors2.Rows[data[i] - 4].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A"));
568
                                            lbTemp = LBLNCM6Temp;
707
                            dtMotors2.Rows[data[i] - 4].SetField(2, data[i + 2].ToString("0 °C"));
569
                                            break;
-
 
570
                                        case 6:
-
 
571
                                            lbCur = LBLNCM7Cur;
-
 
572
                                            lbTemp = LBLNCM7Temp;
708
                        }
573
                                            break;
709
                        else
574
                                        case 7:
710
                        {
575
                                            lbCur = LBLNCM8Cur;
711
                            dtMotors2.Rows[data[i] - 4].SetField(1, "NA");
576
                                            lbTemp = LBLNCM8Temp;
712
                            dtMotors2.Rows[data[i] - 4].SetField(2, "NA");
577
                                            break;
713
                        }
578
                                    }
-
 
579
                                    if (lbCur != null)
714
                    }
580
                                        lbCur.Invoke((Action)(() => lbCur.Text = ((double)data[1] / (double)10).ToString("0.0 A")));
-
 
Line 581... Line 715...
581
                                    if (lbTemp != null)
715
                }
-
 
716
            }
-
 
717
 
-
 
718
        }
-
 
719
        /// <summary>
-
 
720
        /// Navi-Ctrl data 'O'
-
 
721
        /// GPS-Position, capacatiy, flying time...
-
 
722
        /// </summary>
-
 
723
        /// <param name="data">the received byte array to process</param>
-
 
724
        void _processNCData(byte[] data)
-
 
725
        {
-
 
726
            int i_32, i_16, iVal;
-
 
727
            double d;
-
 
728
            i_32 = data[4];
-
 
729
            iVal = i_32 << 24;
-
 
730
            i_32 = data[3];
-
 
731
            iVal += i_32 << 16;
-
 
732
            i_32 = data[2];
-
 
733
            iVal += i_32 << 8;
-
 
734
            iVal += data[1];
Line -... Line 735...
-
 
735
            d = (double)iVal / Math.Pow(10, 7);
-
 
736
            lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = d.ToString("0.######°"))); //GPS-Position: Longitude in decimal degree
-
 
737
            //lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = _convertDegree(d))); //GPS-Position: Longitude in minutes, seconds
582
                                        lbTemp.Invoke((Action)(() => lbTemp.Text = data[2].ToString("0 °C")));
738
 
-
 
739
            i_32 = data[8];
-
 
740
            iVal = i_32 << 24;
-
 
741
            i_32 = data[7];
-
 
742
            iVal += i_32 << 16;
-
 
743
            i_32 = data[6];
-
 
744
            iVal += i_32 << 8;
Line 583... Line -...
583
 
-
 
584
                                }
-
 
585
 
745
            iVal += data[5];
586
                                break;
746
            d = (double)iVal / Math.Pow(10, 7);
587
 
747
            lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = d.ToString("0.######°"))); //GPS-Position: Latitude in decimal degree
588
                            case 'O': //NC Data
748
            //lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = _convertDegree(d))); //GPS-Position: Latitude in minutes, seconds
Line 599... Line 759...
599
                                string Text = t.Hours.ToString("D2") + ":" + t.Minutes.ToString("D2") + ":" + t.Seconds.ToString("D2");
759
            string Text = t.Hours.ToString("D2") + ":" + t.Minutes.ToString("D2") + ":" + t.Seconds.ToString("D2");
600
                                lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = Text.ToString())); //Flying time
760
            lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = Text.ToString())); //Flying time
Line 601... Line 761...
601
 
761
 
602
                                lblNCRC.Invoke((Action)(() => lblNCRC.Text = data[66].ToString())); //RC quality
762
            lblNCRC.Invoke((Action)(() => lblNCRC.Text = data[66].ToString())); //RC quality
603
                                lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = data[69].ToString()));   //NC Errornumber
763
            lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = data[69].ToString()));   //NC Errornumber
604
                                if (data[69] > 0)
764
            //if (data[69] > 0)
605
                                    _readNCError();
765
            //    _readNCError();
606
                                break;
-
 
607
 
766
            //break;
608
                            case 'E': //NC error-string
767
            if (data[69] > 0 & data[69] < 44)
609
                                ErrorLog(LogMsgType.Error, "NC Error: " + s);
-
 
610
 
-
 
Line -... Line 768...
-
 
768
                ErrorLog(LogMsgType.Error, "NC Error [" + data[69].ToString() + "]: " + NC_Error[data[69]]);
-
 
769
 
611
                                break;
770
        }
-
 
771
        /// <summary>
-
 
772
        /// OSD Menue 'L'
-
 
773
        /// single page called by pagenumber
-
 
774
        /// no autoupdate
-
 
775
        /// </summary>
-
 
776
        /// <param name="data">the received byte array to process</param>
612
 
777
        void _processOSDSingle(byte[] data)
613
                            case 'L':
778
        {
614
                                if(data.Length == 84)
779
            if (data.Length == 84)
615
                                {
780
            {
616
                                    string sMessage = "";
781
                string sMessage = "";
Line 625... Line 790...
625
                                    lblOSDPageNr.Invoke((Action)(()=>lblOSDPageNr.Text = iOSDPage.ToString("[0]")));
790
                lblOSDPageNr.Invoke((Action)(() => lblOSDPageNr.Text = iOSDPage.ToString("[0]")));
Line 626... Line 791...
626
 
791
 
627
                                }
792
            }
628
                                else
793
            else
-
 
794
                OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 84)");
-
 
795
 
-
 
796
        }
-
 
797
        /// <summary>
629
                                    OSD(LogMsgType.Incoming,"Wrong length: " + data.Length + " (should be 84)");
798
        /// OSD Menue 'H'
-
 
799
        /// called by keys (0x01,0x02,0x03,0x04)
-
 
800
        /// autoupdate
-
 
801
        /// </summary>
630
                                break;
802
        /// <param name="data">the received byte array to process</param>
-
 
803
        void _processOSDAuto(byte[] data)
631
                            case 'H':
804
        {
632
                                if(data.Length == 81)
805
            if (data.Length == 81)
633
                                {
806
            {
634
                                    string sMessage = "";
807
                string sMessage = "";
635
                                    sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1));
808
                sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1));
Line 639... Line 812...
639
                                    OSD(LogMsgType.Incoming, sMessage.Substring(60,20));
812
                OSD(LogMsgType.Incoming, sMessage.Substring(60, 20));
Line 640... Line 813...
640
 
813
 
641
                                }
814
            }
642
                                else
815
            else
643
                                    OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)");
-
 
644
                                break;
-
 
645
 
-
 
646
                            //default:
-
 
647
                            //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
-
 
648
                            //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
-
 
649
                            //    break;
-
 
650
                        }
-
 
651
                    }
-
 
652
                    //else
-
 
653
                    //{
-
 
654
                    //    Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString());
-
 
655
                    //    Log(LogMsgType.Incoming, BitConverter.ToString(data));
-
 
656
                    //}
-
 
657
                }
-
 
658
            }
816
                OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)");
-
 
817
        }
Line 659... Line 818...
659
        }
818
        #endregion processing received data
660
 
819
 
661
        /// <summary> send message to controller to request data
820
        /// <summary> send message to controller to request data
662
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
821
        /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/
Line 911... Line 1070...
911
            bReadContinously = b;
1070
            bReadContinously = b;
912
            btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.Text = bReadContinously ? "stop automatic" + Environment.NewLine + "data refresh" : "start automatic" + Environment.NewLine + "data refresh"));
1071
            btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.Text = bReadContinously ? "stop automatic" + Environment.NewLine + "data refresh" : "start automatic" + Environment.NewLine + "data refresh"));
913
            btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.BackColor = bReadContinously ? Color.FromArgb(192, 255, 192) : Color.FromArgb(224, 224, 224)));
1072
            btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.BackColor = bReadContinously ? Color.FromArgb(192, 255, 192) : Color.FromArgb(224, 224, 224)));
914
            if (bReadContinously)
1073
            if (bReadContinously)
915
            {
1074
            {
916
                _readDebugData(true);
1075
                if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); }
917
                if (_iCtrlAct == 2) { Thread.Sleep(10); _readBLCtrl(true);}
1076
                if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); }
918
                if (_iCtrlAct == 2) { Thread.Sleep(10); _readNavData(true);}
1077
                if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); }
919
                Thread.Sleep(10);
-
 
920
                _OSDMenueAutoRefresh();
1078
                if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); Thread.Sleep(10);}
921
                lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0)));
1079
                lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0)));
922
            }
1080
            }
923
            else
1081
            else
924
                lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(224, 224, 224)));
1082
                lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(224, 224, 224)));
925
            _iLifeCounter = 0;
1083
            _iLifeCounter = 0;
926
        }
1084
        }
927
        /// <summary>
1085
        /// <summary>
928
        /// set fieldtexts to "NA" when not available with FC
1086
        /// set values to "NA" when not available with FC
929
        /// </summary>
1087
        /// </summary>
930
        void _setFieldsNA()
1088
        void _setFieldsNA()
931
        {
1089
        {
932
            Thread.Sleep(100);
1090
            Thread.Sleep(100);
933
            Label lbCur = new Label(), lbTemp = new Label();
-
 
934
            for (int i = 0; i < 8; i++)
-
 
935
            {
-
 
936
                //BL-Ctrl Temp & Current
-
 
937
                switch (i)
1091
            _initDTMotors();
938
                {
-
 
939
                    case 0:
-
 
940
                        lbCur = LBLNCM1Cur;
-
 
941
                        lbTemp = LBLNCM1Temp;
-
 
942
                        break;
-
 
943
                    case 1:
-
 
944
                        lbCur = LBLNCM2Cur;
-
 
945
                        lbTemp = LBLNCM2Temp;
-
 
946
                        break;
-
 
947
                    case 2:
-
 
948
                        lbCur = LBLNCM3Cur;
-
 
949
                        lbTemp = LBLNCM3Temp;
-
 
950
                        break;
-
 
951
                    case 3:
-
 
952
                        lbCur = LBLNCM4Cur;
-
 
953
                        lbTemp = LBLNCM4Temp;
-
 
954
                        break;
-
 
955
                    case 4:
-
 
956
                        lbCur = LBLNCM5Cur;
-
 
957
                        lbTemp = LBLNCM5Temp;
-
 
958
                        break;
-
 
959
                    case 5:
-
 
960
                        lbCur = LBLNCM6Cur;
-
 
961
                        lbTemp = LBLNCM6Temp;
-
 
962
                        break;
-
 
963
                    case 6:
-
 
964
                        lbCur = LBLNCM7Cur;
-
 
965
                        lbTemp = LBLNCM7Temp;
-
 
966
                        break;
-
 
967
                    case 7:
-
 
968
                        lbCur = LBLNCM8Cur;
-
 
969
                        lbTemp = LBLNCM8Temp;
-
 
970
                        break;
-
 
971
                }
-
 
972
                if (lbCur != null)
-
 
973
                    lbCur.Invoke((Action)(() => lbCur.Text = "NA"));
-
 
974
                if (lbTemp != null)
-
 
975
                    lbTemp.Invoke((Action)(() => lbTemp.Text = "NA"));
-
 
976
 
-
 
977
            }
-
 
978
            lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = "NA"));    //FlightTime
1092
            lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = "NA"));    //FlightTime
979
            lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = "NA")); //NC ErrorNr
1093
            lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = "NA"));  //NC ErrorNr
980
            lblNCMF.Invoke((Action)(() => lblNCMF.Text = "NA"));           //earth magnet field
1094
            lblNCMF.Invoke((Action)(() => lblNCMF.Text = "NA"));            //earth magnet field
981
            lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = "NA")); //GroundSpeed
1095
            lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = "NA"));    //GroundSpeed
982
            lblNCDist.Invoke((Action)(() => lblNCDist.Text = "NA")); //Distance to HP
1096
            lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = "NA"));    //Distance to HP
983
            lblNCSat.Invoke((Action)(() => lblNCSat.Text = "NA"));      //Sats used
1097
            lblNCSat.Invoke((Action)(() => lblNCSat.Text = "NA"));          //Sats used
-
 
1098
            lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = "NA"));  //GPS position - longitude
-
 
1099
            lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = "NA"));    //GPS position - latitude
984
        }
1100
        }
985
        /// <summary>
1101
        /// <summary>
986
        /// one time query of the OSD Menue with pagenumber
1102
        /// one time query of the OSD Menue with pagenumber
987
        /// </summary>
1103
        /// </summary>
988
        /// <param name="iMenue">Menue page</param>
1104
        /// <param name="iMenue">Menue page</param>
Line 1075... Line 1191...
1075
            ini.IniWriteValue("default", "IntervalNavCtrlData", navctrlInterval.ToString());
1191
            ini.IniWriteValue("default", "IntervalNavCtrlData", navctrlInterval.ToString());
1076
            ini.IniWriteValue("default", "IntervalBLCtrlData", blctrlInterval.ToString());
1192
            ini.IniWriteValue("default", "IntervalBLCtrlData", blctrlInterval.ToString());
1077
            ini.IniWriteValue("default", "IntervalOSDData", OSDInterval.ToString());
1193
            ini.IniWriteValue("default", "IntervalOSDData", OSDInterval.ToString());
1078
        }
1194
        }
Line -... Line 1195...
-
 
1195
 
-
 
1196
        /// <summary>
-
 
1197
        /// initialize the 2 datatables for motor values
-
 
1198
        /// dtMotors1 - motor 1 - 4
-
 
1199
        /// dtMotors2 - motor 5 - 8
-
 
1200
        /// DataGridView dgvMotors1/2 are bound to dtMotors1/2 
-
 
1201
        /// </summary>
-
 
1202
        void _initDTMotors()
-
 
1203
        {
-
 
1204
            for(int i = 0; i < 4; i++)
-
 
1205
            {
-
 
1206
                if (dtMotors1.Rows.Count < 4)
-
 
1207
                    dtMotors1.Rows.Add((i + 1).ToString(), "NA", "NA");
-
 
1208
                else
-
 
1209
                {
-
 
1210
                    dtMotors1.Rows[i].SetField(1, "NA");
-
 
1211
                    dtMotors1.Rows[i].SetField(2, "NA");
-
 
1212
                }
-
 
1213
                if (dtMotors2.Rows.Count < 4)
-
 
1214
                    dtMotors2.Rows.Add((i + 5).ToString(), "NA", "NA");
-
 
1215
                else
-
 
1216
                {
-
 
1217
                    dtMotors2.Rows[i].SetField(1, "NA");
-
 
1218
                    dtMotors2.Rows[i].SetField(2, "NA");
-
 
1219
                }
-
 
1220
            }
-
 
1221
            dgvMotors1.Invoke((Action)(() => dgvMotors1.Refresh()));
-
 
1222
            dgvMotors2.Invoke((Action)(() => dgvMotors2.Refresh()));
-
 
1223
        }
-
 
1224
 
-
 
1225
        /// <summary>
-
 
1226
        /// Convert decimal degrees to degrees, minutes, seconds, milliseconds 
-
 
1227
        /// </summary>
-
 
1228
        /// <param name="coord">the degree value as double</param>
-
 
1229
        /// <returns>0° 0' 0,0"</returns>
-
 
1230
        string _convertDegree(double coord)
-
 
1231
        {
-
 
1232
            //double minutes = (degree - Math.Floor(degree)) * 60.0;
-
 
1233
            //double seconds = (minutes - Math.Floor(minutes)) * 60.0;
-
 
1234
            //double tenths = (seconds - Math.Floor(seconds)) * 10.0;
-
 
1235
            //// get rid of fractional part
-
 
1236
            //minutes = Math.Floor(minutes);
-
 
1237
            //seconds = Math.Floor(seconds);
-
 
1238
            //tenths = Math.Floor(tenths);
-
 
1239
 
-
 
1240
 
-
 
1241
            //int sec = (int)Math.Round(coord * 3600);
-
 
1242
            //int deg = sec / 3600;
-
 
1243
            //sec = Math.Abs(sec % 3600);
-
 
1244
            //int min = sec / 60;
-
 
1245
            //sec %= 60;
-
 
1246
 
-
 
1247
            var ts = TimeSpan.FromHours(Math.Abs(coord));
-
 
1248
            double deg = Math.Sign(coord) * Math.Floor(ts.TotalHours);
-
 
1249
            int min = ts.Minutes;
-
 
1250
            int sec = ts.Seconds;
-
 
1251
            int milli = ts.Milliseconds;
-
 
1252
 
-
 
1253
            return deg.ToString("0° ") + min.ToString("0") + "' " + sec.ToString("0") + "," + milli.ToString() + "\"";
1079
 
1254
        }
Line 1080... Line 1255...
1080
        #endregion functions
1255
        #endregion functions
1081
 
1256
 
1082
        #region buttons
1257
        #region buttons
Line 1197... Line 1372...
1197
        private void btnOSDEnter_Click(object sender, EventArgs e)
1372
        private void btnOSDEnter_Click(object sender, EventArgs e)
1198
        {
1373
        {
1199
            _OSDMenueAutoRefresh(4);
1374
            _OSDMenueAutoRefresh(4);
1200
        }
1375
        }
1201
        #endregion buttons
1376
        #endregion buttons
1202
 
-
 
1203
    }
1377
    }
1204
    public class IniFile
1378
    public class IniFile
1205
    {
1379
    {
1206
        public string path;
1380
        public string path;