Rev 2254 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2233 | - | 1 | ///============================================================================ |
2 | /// MKLiveView |
||
3 | /// Copyright © 2016 Steph |
||
4 | /// |
||
5 | ///This file is part of MKLiveView. |
||
6 | /// |
||
7 | ///MKLiveView is free software: you can redistribute it and/or modify |
||
8 | ///it under the terms of the GNU General Public License as published by |
||
9 | ///the Free Software Foundation, either version 3 of the License, or |
||
10 | ///(at your option) any later version. |
||
11 | /// |
||
12 | ///MKLiveView is distributed in the hope that it will be useful, |
||
13 | ///but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | ///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | ///GNU General Public License for more details. |
||
16 | /// |
||
17 | ///You should have received a copy of the GNU General Public License |
||
18 | ///along with cssRcon. If not, see <http://www.gnu.org/licenses/>. |
||
19 | /// |
||
20 | ///============================================================================ |
||
21 | ///Credits: |
||
22 | ///Chootair (http://www.codeproject.com/script/Membership/View.aspx?mid=3941737) |
||
23 | ///for his "C# Avionic Instrument Controls" (http://www.codeproject.com/Articles/27411/C-Avionic-Instrument-Controls) |
||
24 | ///I used some of his code for displaying the compass |
||
25 | /// |
||
26 | ///Tom Pyke (http://tom.pycke.be) |
||
27 | ///for his "Artifical horizon" (http://tom.pycke.be/mav/100/artificial-horizon) |
||
28 | ///Great job! |
||
29 | /// |
||
30 | /// and last but most of all to JOHN C. MACDONALD at Ira A. Fulton College of Engineering and Technology |
||
31 | /// for his MIKROKOPTER SERIAL CONTROL TUTORIAL (http://hdl.lib.byu.edu/1877/2747) |
||
32 | /// and the sourcode (http://hdl.lib.byu.edu/1877/2748) |
||
33 | /// By his work I finally managed to get the communication with the Mikrokopter controllers to run |
||
34 | /// Some of his code was used in this programm like the SimpelSerialPort class (with some changes) |
||
35 | /// and the FilghtControllerMessage class |
||
36 | /// |
||
37 | ///============================================================================ |
||
38 | |||
39 | using System; |
||
40 | using System.Data; |
||
41 | using System.Drawing; |
||
42 | using System.Text; |
||
43 | using System.Windows.Forms; |
||
44 | using System.IO; |
||
45 | using System.Threading; |
||
46 | using System.Diagnostics; |
||
47 | using System.Runtime.InteropServices; |
||
48 | |||
49 | namespace MKLiveView |
||
50 | { |
||
51 | public partial class MainForm : Form |
||
52 | { |
||
2250 | - | 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" |
||
99 | }; |
||
2233 | - | 100 | |
101 | [FlagsAttribute] |
||
102 | enum NC_HWError0 : short |
||
103 | { |
||
104 | None = 0, |
||
105 | SPI_RX = 1, |
||
106 | COMPASS_RX = 2, |
||
107 | FC_INCOMPATIBLE = 4, |
||
108 | COMPASS_INCOMPATIBLE = 8, |
||
109 | GPS_RX = 16, |
||
110 | COMPASS_VALUE = 32 |
||
111 | }; |
||
112 | [FlagsAttribute] |
||
113 | enum FC_HWError0 : short |
||
114 | { |
||
115 | None = 0, |
||
116 | GYRO_NICK = 1, |
||
117 | GYRO_ROLL = 2, |
||
118 | GYRO_YAW = 4, |
||
119 | ACC_NICK = 8, |
||
120 | ACC_ROLL = 16, |
||
121 | ACC_TOP = 32, |
||
122 | PRESSURE = 64, |
||
123 | CAREFREE = 128 |
||
124 | }; |
||
125 | [FlagsAttribute] |
||
126 | enum FC_HWError1 : short |
||
127 | { |
||
128 | None = 0, |
||
129 | I2C = 1, |
||
130 | BL_MISSING = 2, |
||
131 | SPI_RX = 4, |
||
132 | PPM = 8, |
||
133 | MIXER = 16, |
||
134 | RC_VOLTAGE = 32, |
||
135 | ACC_NOT_CAL = 64, |
||
136 | RES3 = 128 |
||
137 | }; |
||
138 | public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error }; |
||
139 | // Various colors for logging info |
||
140 | private Color[] LogMsgTypeColor = { Color.FromArgb(43, 145, 175), Color.Green, Color.Black, Color.Orange, Color.Red }; |
||
141 | |||
142 | string[] sAnalogLabel = new string[32]; |
||
143 | string[] sAnalogData = new string[32]; |
||
144 | bool bReadContinously = false; |
||
145 | bool check_HWError = false; |
||
146 | bool _bCBInit = true; |
||
147 | bool _init = true; |
||
148 | bool _debugDataAutorefresh = true; |
||
149 | bool _navCtrlDataAutorefresh = true; |
||
150 | bool _blctrlDataAutorefresh = true; |
||
151 | bool _OSDAutorefresh = true; |
||
2254 | - | 152 | bool _bErrorLog = false; |
2233 | - | 153 | int crcError = 0; |
154 | int iLableIndex = 0; |
||
155 | string filePath = Directory.GetCurrentDirectory(); |
||
156 | string fileName = "NCLabelTexts.txt"; |
||
157 | int _iCtrlAct = 0; |
||
158 | int _iLifeCounter = 0; |
||
159 | int iOSDPage = 0; |
||
160 | int iOSDMax = 0; |
||
2257 | - | 161 | int[] serChan = new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
162 | string[] serChanTitle = new string[12]; |
||
2233 | - | 163 | /// <summary> |
164 | /// interval for sending debugdata (multiplied by 10ms) |
||
165 | /// </summary> |
||
166 | byte debugInterval = 25; //(=> 250ms) |
||
167 | /// <summary> |
||
168 | /// interval for sending BL-CTRL status (multiplied by 10ms) |
||
169 | /// </summary> |
||
170 | byte blctrlInterval = 45; |
||
171 | /// <summary> |
||
172 | /// interval for sending NAV-CTRL status (multiplied by 10ms) |
||
173 | /// </summary> |
||
174 | byte navctrlInterval = 80; |
||
175 | /// <summary> |
||
176 | /// interval for sending OSD page update (multiplied by 10ms) |
||
177 | /// </summary> |
||
178 | byte OSDInterval = 85; |
||
179 | /// <summary> |
||
180 | /// datatable for the debug data array - displayed on settings tabpage in datagridview |
||
181 | /// </summary> |
||
182 | DataTable dtAnalog = new DataTable(); |
||
2254 | - | 183 | /// <summary> |
184 | /// datatable for motordata (current,temp) |
||
185 | /// </summary> |
||
2250 | - | 186 | DataTable dtMotors1 = new DataTable(); |
187 | DataTable dtMotors2 = new DataTable(); |
||
188 | |||
2257 | - | 189 | DataTable dtWaypoints = new DataTable(); |
190 | |||
2233 | - | 191 | public MainForm() |
192 | { |
||
193 | InitializeComponent(); |
||
2257 | - | 194 | serChanTitle.Initialize(); |
195 | |||
2233 | - | 196 | _readIni(); |
2257 | - | 197 | |
2233 | - | 198 | dtAnalog.Columns.Add("ID"); |
199 | dtAnalog.Columns.Add("Value"); |
||
200 | dataGridView1.DataSource = dtAnalog; |
||
2257 | - | 201 | |
2250 | - | 202 | dtMotors1.Columns.Add("#"); |
203 | dtMotors1.Columns.Add("Current"); |
||
204 | dtMotors1.Columns.Add("Temp"); |
||
205 | dtMotors2.Columns.Add("#"); |
||
206 | dtMotors2.Columns.Add("Current"); |
||
207 | dtMotors2.Columns.Add("Temp"); |
||
208 | dgvMotors1.DataSource = dtMotors1; |
||
209 | dgvMotors2.DataSource = dtMotors2; |
||
210 | _initDTMotors(); |
||
211 | dgvMotors1.Columns[0].Width = 24; |
||
212 | dgvMotors1.Columns[1].Width = 74; |
||
213 | dgvMotors1.Columns[2].Width = 74; |
||
214 | dgvMotors2.Columns[0].Width = 24; |
||
215 | dgvMotors2.Columns[1].Width = 74; |
||
216 | dgvMotors2.Columns[2].Width = 74; |
||
2257 | - | 217 | |
218 | dtWaypoints.Columns.Add("Index"); |
||
219 | dtWaypoints.Columns.Add("Type"); |
||
220 | dtWaypoints.Columns.Add("Name"); |
||
221 | dtWaypoints.Columns.Add("Latitude"); |
||
222 | dtWaypoints.Columns.Add("Longitude"); |
||
223 | dtWaypoints.Columns.Add("Altitude"); |
||
224 | dtWaypoints.Columns.Add("Heading"); |
||
225 | dtWaypoints.Columns.Add("Speed"); |
||
226 | dtWaypoints.Columns.Add("Altitude rate"); |
||
227 | dtWaypoints.Columns.Add("Tol.radius"); |
||
228 | dtWaypoints.Columns.Add("Hold time"); |
||
229 | dtWaypoints.Columns.Add("AutoTrigger"); |
||
230 | dtWaypoints.Columns.Add("Cam angle"); |
||
231 | dtWaypoints.Columns.Add("Event"); |
||
232 | dtWaypoints.Columns.Add("Eventchan. Val."); |
||
233 | dtWaypoints.Columns.Add("Status"); |
||
234 | dgvWP.DataSource = dtWaypoints; |
||
235 | |||
2233 | - | 236 | simpleSerialPort.PortClosed += SimpleSerialPort_PortClosed; |
237 | simpleSerialPort.PortOpened += SimpleSerialPort_PortOpened; |
||
238 | simpleSerialPort.DataReceived += processMessage; |
||
239 | chkbAutoBL.Checked = _blctrlDataAutorefresh; |
||
240 | chkbAutoDbg.Checked = _debugDataAutorefresh; |
||
241 | chkbAutoNav.Checked = _navCtrlDataAutorefresh; |
||
242 | chkbAutoOSD.Checked = _OSDAutorefresh; |
||
243 | labelTimingDebug.Text = (debugInterval * 10).ToString(); |
||
244 | labelTimingBLCTRL.Text = (blctrlInterval * 10).ToString(); |
||
245 | labelTimingNAV.Text = (navctrlInterval * 10).ToString(); |
||
246 | labelTimingOSD.Text = (OSDInterval * 10).ToString(); |
||
2257 | - | 247 | TabControl1.TabPages.Remove(tabPageTesting); |
2233 | - | 248 | } |
2257 | - | 249 | |
2233 | - | 250 | #region events |
251 | private void MainForm_Shown(object sender, EventArgs e) |
||
252 | { |
||
253 | _loadLabelNames(); |
||
2257 | - | 254 | _initSerialCtrl(); |
2233 | - | 255 | _init = false; |
2250 | - | 256 | splitContainer1.SplitterDistance = 514; |
2233 | - | 257 | } |
258 | private void MainForm_FormClosed(object sender, FormClosedEventArgs e) |
||
259 | { |
||
260 | _writeIni(); |
||
261 | } |
||
262 | private void SimpleSerialPort_PortOpened() |
||
263 | { |
||
264 | btnConn.Invoke((Action)(() => btnConn.BackColor = Color.FromArgb(192, 255, 192))); |
||
265 | btnConn.Invoke((Action)(() => btnConn.Text = "close" + Environment.NewLine + "serial port")); |
||
266 | _getVersion(); |
||
267 | Thread.Sleep(100); |
||
268 | _OSDMenue(0); |
||
2257 | - | 269 | Thread.Sleep(200); |
270 | _sendSerialData(); |
||
2233 | - | 271 | // _readCont(true); |
272 | } |
||
273 | private void SimpleSerialPort_PortClosed() |
||
274 | { |
||
275 | btnConn.Invoke((Action)(() => btnConn.BackColor = Color.FromArgb(224, 224, 224))); |
||
276 | btnConn.Invoke((Action)(() => btnConn.Text = "open" + Environment.NewLine + "serial port")); |
||
277 | _readCont(false); |
||
278 | } |
||
279 | /// <summary> |
||
280 | /// timer for refreshing subscription of subscribed data |
||
281 | /// query lifecounter for connection failure |
||
282 | /// </summary> |
||
283 | private void timer1_Tick(object sender, EventArgs e) |
||
284 | { |
||
285 | if(bReadContinously) |
||
286 | { |
||
287 | if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); } |
||
288 | |||
2250 | - | 289 | if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); } |
2233 | - | 290 | |
291 | if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); } |
||
292 | check_HWError = true; |
||
293 | _getVersion(); |
||
294 | Thread.Sleep(10); |
||
295 | if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); } |
||
296 | if (_iLifeCounter > 0) |
||
297 | { |
||
298 | lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0); |
||
299 | _iLifeCounter = 0; |
||
300 | } |
||
301 | else |
||
302 | { |
||
303 | Log(LogMsgType.Error, "No communication to NC/FC!"); |
||
304 | lblLifeCounter.BackColor = Color.FromArgb(224, 0, 0); |
||
305 | } |
||
306 | } |
||
307 | } |
||
308 | private void cbOSD_SelectedIndexChanged(object sender, EventArgs e) |
||
309 | { |
||
310 | if (!_bCBInit && cbOSD.SelectedIndex > -1) |
||
311 | _OSDMenue(cbOSD.SelectedIndex); |
||
312 | } |
||
313 | private void chkbAutoDbg_CheckedChanged(object sender, EventArgs e) |
||
314 | { |
||
315 | if(!_init) _debugDataAutorefresh = chkbAutoDbg.Checked; |
||
316 | } |
||
317 | private void chkbAutoNav_CheckedChanged(object sender, EventArgs e) |
||
318 | { |
||
319 | if (!_init) _navCtrlDataAutorefresh = chkbAutoNav.Checked; |
||
320 | } |
||
321 | private void chkbAutoBL_CheckedChanged(object sender, EventArgs e) |
||
322 | { |
||
323 | if (!_init) _blctrlDataAutorefresh = chkbAutoBL.Checked; |
||
324 | } |
||
325 | private void chkbAutoOSD_CheckedChanged(object sender, EventArgs e) |
||
326 | { |
||
327 | if (!_init) _OSDAutorefresh = chkbAutoOSD.Checked; |
||
328 | } |
||
329 | private void cbTimingDebug_SelectedIndexChanged(object sender, EventArgs e) |
||
330 | { |
||
331 | if (cbTimingDebug.SelectedIndex > -1) |
||
332 | { |
||
333 | debugInterval = (byte)(Convert.ToInt16(cbTimingDebug.SelectedItem) / 10); |
||
334 | labelTimingDebug.Text = (debugInterval * 10).ToString(); |
||
335 | } |
||
336 | } |
||
337 | private void cbTimingNAV_SelectedIndexChanged(object sender, EventArgs e) |
||
338 | { |
||
339 | if (cbTimingNAV.SelectedIndex > -1) |
||
340 | { |
||
341 | navctrlInterval = (byte)(Convert.ToInt16(cbTimingNAV.SelectedItem) / 10); |
||
342 | labelTimingNAV.Text = (navctrlInterval * 10).ToString(); |
||
343 | } |
||
344 | } |
||
345 | private void cbTimingBLCTRL_SelectedIndexChanged(object sender, EventArgs e) |
||
346 | { |
||
347 | if (cbTimingBLCTRL.SelectedIndex > -1) |
||
348 | { |
||
349 | blctrlInterval = (byte)(Convert.ToInt16(cbTimingBLCTRL.SelectedItem) / 10); |
||
350 | labelTimingBLCTRL.Text = (blctrlInterval * 10).ToString(); |
||
351 | } |
||
352 | } |
||
353 | private void cbTimingOSD_SelectedIndexChanged(object sender, EventArgs e) |
||
354 | { |
||
355 | if (cbTimingOSD.SelectedIndex > -1) |
||
356 | { |
||
357 | OSDInterval = (byte)(Convert.ToInt16(cbTimingOSD.SelectedItem) / 10); |
||
358 | labelTimingOSD.Text = (OSDInterval * 10).ToString(); |
||
359 | } |
||
360 | } |
||
2254 | - | 361 | private void rtfError_LinkClicked(object sender, LinkClickedEventArgs e) |
362 | { |
||
363 | System.Diagnostics.Process.Start(e.LinkText); |
||
364 | } |
||
2257 | - | 365 | |
2233 | - | 366 | #endregion events |
367 | |||
368 | /// <summary> Log data to the terminal window. </summary> |
||
369 | /// <param name="msgtype"> The type of message to be written. </param> |
||
370 | /// <param name="msg"> The string containing the message to be shown. </param> |
||
371 | private void Log(LogMsgType msgtype, string msg) |
||
372 | { |
||
373 | rtfTerminal.Invoke(new EventHandler(delegate |
||
374 | { |
||
375 | if (rtfTerminal.Lines.Length >= 1000) //Wenn Terminal mehr als 1000 Zeilen hat |
||
376 | rtfTerminal.Select(42, (500 * 129)); //500 löschen |
||
377 | rtfTerminal.Select(rtfTerminal.Text.Length, 0); |
||
378 | rtfTerminal.SelectedText = string.Empty; |
||
379 | rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont, FontStyle.Regular); |
||
380 | rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype]; |
||
381 | rtfTerminal.AppendText(msg + Environment.NewLine); |
||
382 | rtfTerminal.ScrollToCaret(); |
||
383 | })); |
||
384 | } |
||
385 | /// <summary> display the OSD text in 4 lines à 20 chars </summary> |
||
386 | /// <param name="msgtype"> The type of message to be written. </param> |
||
387 | /// <param name="msg"> The string containing the message to be shown. </param> |
||
388 | private void OSD(LogMsgType msgtype, string msg) |
||
389 | { |
||
390 | rtfOSD.Invoke(new EventHandler(delegate |
||
391 | { |
||
392 | if (rtfOSD.Lines.Length > 4) |
||
393 | rtfOSD.Clear(); |
||
394 | rtfOSD.Select(rtfOSD.Text.Length,0); |
||
395 | rtfOSD.SelectedText = string.Empty; |
||
396 | rtfOSD.SelectionFont = new Font(rtfOSD.SelectionFont, FontStyle.Regular); |
||
397 | rtfOSD.SelectionColor = LogMsgTypeColor[(int)msgtype]; |
||
398 | rtfOSD.AppendText(msg + Environment.NewLine); |
||
399 | if (rtfOSD.Text.IndexOf("ERR") > 0) |
||
400 | { |
||
401 | rtfOSD.Select(rtfOSD.Text.IndexOf("ERR"), 40); |
||
402 | rtfOSD.SelectionColor = LogMsgTypeColor[(int)LogMsgType.Error]; |
||
403 | } |
||
404 | })); |
||
405 | } |
||
406 | private void ErrorLog(LogMsgType msgtype, string msg) |
||
407 | { |
||
408 | rtfError.Invoke(new EventHandler(delegate |
||
409 | { |
||
410 | if (rtfError.Lines.Length > 4) |
||
411 | rtfError.Clear(); |
||
412 | rtfError.Focus(); |
||
413 | rtfError.Select(rtfError.Text.Length, 0); |
||
414 | rtfError.SelectedText = string.Empty; |
||
415 | rtfError.SelectionFont = new Font(rtfError.SelectionFont, FontStyle.Regular); |
||
416 | rtfError.SelectionColor = LogMsgTypeColor[(int)msgtype]; |
||
417 | rtfError.AppendText(msg + Environment.NewLine); |
||
418 | |||
419 | })); |
||
2254 | - | 420 | _bErrorLog = true; |
2233 | - | 421 | } |
422 | |||
423 | #region functions |
||
424 | |||
2250 | - | 425 | #region processing received data |
426 | /// <summary> Processing the messages and displaying them in the according form controls |
||
427 | /// function called by simpleSerialPort.DataReceived event |
||
428 | /// </summary> |
||
2233 | - | 429 | /// <param name="message"> message bytearray recieved by SimpleSerialPort class </param> |
430 | private void processMessage(byte[] message) |
||
431 | { |
||
432 | if (message.Length > 0) |
||
433 | { |
||
434 | _iLifeCounter++; |
||
435 | //Log(LogMsgType.Incoming, BitConverter.ToString(message)); |
||
436 | //Log(LogMsgType.Incoming, message.Length.ToString()); |
||
437 | string s = new string(ASCIIEncoding.ASCII.GetChars(message, 0, message.Length)); |
||
438 | char cmdID; |
||
439 | byte adr; |
||
440 | byte[] data; |
||
2257 | - | 441 | byte[] tmp = null; |
2233 | - | 442 | if (message[0] != '#') |
2257 | - | 443 | { |
444 | int iFound = -1; |
||
445 | for(int i=0;i<message.Length;i++) //Sometimes the FC/NC sends strings without termination (like WP messages) |
||
446 | { //so this is a workaround to not spam the log box |
||
447 | if (message[i] == 35) |
||
448 | { |
||
449 | iFound = i; |
||
450 | break; |
||
451 | } |
||
452 | } |
||
453 | if(iFound>0) |
||
454 | { |
||
455 | s = new string(ASCIIEncoding.ASCII.GetChars(message, 0,iFound)); |
||
456 | tmp = new byte[message.Length - iFound]; |
||
457 | Buffer.BlockCopy(message, iFound, tmp, 0, message.Length - iFound); |
||
458 | } |
||
459 | s = s.Trim('\0', '\n', '\r'); |
||
460 | if(s.Length > 0) |
||
461 | Log(LogMsgType.Normal, s); |
||
462 | if (tmp != null) |
||
463 | { |
||
464 | s = new string(ASCIIEncoding.ASCII.GetChars(tmp, 0, tmp.Length)); |
||
465 | processMessage(tmp); |
||
466 | } |
||
467 | } |
||
2233 | - | 468 | //Debug.Print(s); |
469 | else |
||
470 | { |
||
471 | FlightControllerMessage.ParseMessage(message, out cmdID, out adr, out data); |
||
472 | |||
473 | if (adr == 255) { crcError++; } |
||
474 | else crcError = 0; |
||
475 | lblCRCErr.Invoke((Action)(() => lblCRCErr.Text = crcError.ToString())); |
||
2250 | - | 476 | //display the active controller (FC / NC) |
2233 | - | 477 | 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...??? |
478 | { |
||
479 | _iCtrlAct = adr; |
||
480 | switch (adr) |
||
481 | { |
||
482 | case 1: |
||
483 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "FC")); |
||
484 | lblNCCtrl.Invoke((Action)(() => lblNCCtrl.Text = "FC")); |
||
485 | _setFieldsNA(); //display fields NA for FC |
||
486 | break; |
||
487 | case 2: |
||
488 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "NC")); |
||
489 | lblNCCtrl.Invoke((Action)(() => lblNCCtrl.Text = "NC")); |
||
490 | break; |
||
491 | case 3: |
||
492 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "MK3MAG")); |
||
493 | break; |
||
494 | case 4: |
||
495 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "BL-CTRL")); |
||
496 | break; |
||
497 | default: |
||
498 | lblCtrl.Invoke((Action)(() => lblCtrl.Text = "....")); |
||
499 | break; |
||
500 | } |
||
2257 | - | 501 | _loadLabelNames(); |
2233 | - | 502 | } |
2257 | - | 503 | // else |
504 | // Debug.Print("Address == 0?"); |
||
2233 | - | 505 | |
506 | if (data != null && data.Length > 0) |
||
507 | { |
||
508 | s = new string(ASCIIEncoding.ASCII.GetChars(data, 1, data.Length - 1)); |
||
509 | s = s.Trim('\0', '\n'); |
||
510 | |||
511 | switch (cmdID) |
||
512 | { |
||
2250 | - | 513 | case 'A': //Label names |
514 | _processLabelNames(s); |
||
2233 | - | 515 | break; |
516 | |||
2250 | - | 517 | case 'D': //Debug data |
2257 | - | 518 | _processDebugVals(adr, data); |
2233 | - | 519 | break; |
520 | |||
2250 | - | 521 | case 'V': //Version |
522 | _processVersion(adr, data); |
||
2233 | - | 523 | break; |
524 | |||
2250 | - | 525 | case 'K'://BL-CTRL data |
526 | _processBLCtrl(data); |
||
2233 | - | 527 | break; |
528 | |||
529 | case 'O': //NC Data |
||
2250 | - | 530 | _processNCData(data); |
2233 | - | 531 | break; |
532 | |||
533 | case 'E': //NC error-string |
||
534 | ErrorLog(LogMsgType.Error, "NC Error: " + s); |
||
535 | break; |
||
536 | |||
2250 | - | 537 | case 'L': //OSD Menue (called by pagenumber) |
538 | _processOSDSingle(data); |
||
2233 | - | 539 | break; |
540 | |||
2250 | - | 541 | case 'H': //OSD Menue (with autoupdate - called by Key) |
542 | _processOSDAuto(data); |
||
2233 | - | 543 | break; |
544 | |||
2257 | - | 545 | case 'X': //Waypoint data |
546 | _processWPData(data); |
||
547 | break; |
||
548 | |||
2233 | - | 549 | //default: |
550 | // Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString()); |
||
551 | // Log(LogMsgType.Incoming, BitConverter.ToString(data)); |
||
552 | // break; |
||
553 | } |
||
554 | } |
||
555 | //else |
||
556 | //{ |
||
557 | // Log(LogMsgType.Incoming, "cmd: " + cmdID.ToString()); |
||
558 | // Log(LogMsgType.Incoming, BitConverter.ToString(data)); |
||
559 | //} |
||
560 | } |
||
561 | } |
||
562 | } |
||
2250 | - | 563 | /// <summary> |
564 | /// Analog label names 'A' |
||
565 | /// each label name is returned as a single string |
||
566 | /// and added to string array sAnalogLabel[] |
||
567 | /// and the datatable dtAnalog |
||
568 | /// </summary> |
||
569 | /// <param name="s">the label name</param> |
||
570 | void _processLabelNames(string s) |
||
571 | { |
||
572 | if (iLableIndex < 32) |
||
573 | { |
||
574 | sAnalogLabel[iLableIndex] = s; |
||
575 | if (dtAnalog.Rows.Count < 32) |
||
576 | dtAnalog.Rows.Add(s, ""); |
||
577 | else |
||
578 | dtAnalog.Rows[iLableIndex].SetField(0, s); |
||
2233 | - | 579 | |
2250 | - | 580 | _getAnalogLabels(iLableIndex + 1); |
581 | } |
||
582 | Debug.Print(s); |
||
583 | } |
||
584 | /// <summary> |
||
585 | /// Debug values 'D' |
||
586 | /// </summary> |
||
587 | /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param> |
||
588 | /// <param name="data">the received byte array to process</param> |
||
589 | void _processDebugVals(byte adr,byte[] data) |
||
590 | { |
||
591 | if (data.Length == 66) |
||
592 | { |
||
593 | int[] iAnalogData = new int[32]; |
||
594 | |||
595 | int index = 0; |
||
596 | Int16 i16 = 0; |
||
597 | double dTemp = 0; |
||
598 | for (int i = 2; i < 66; i += 2) |
||
599 | { |
||
600 | i16 = data[i + 1]; |
||
601 | i16 = (Int16)(i16 << 8); |
||
602 | iAnalogData[index] = data[i] + i16; |
||
603 | sAnalogData[index] = (data[i] + i16).ToString(); |
||
604 | dtAnalog.Rows[index].SetField(1, sAnalogData[index]); |
||
605 | |||
606 | if (adr == 2) //NC |
||
607 | { |
||
608 | switch (index) |
||
609 | { |
||
610 | case 0: //pitch (German: nick) |
||
611 | artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10))); |
||
612 | lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
||
613 | break; |
||
614 | case 1: //roll |
||
615 | artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10))); |
||
616 | lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
||
617 | break; |
||
618 | case 4: //altitude |
||
619 | lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"))); |
||
620 | break; |
||
621 | case 7: //Voltage |
||
622 | lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V"))); |
||
623 | break; |
||
624 | case 8: // Current |
||
625 | lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"))); |
||
626 | break; |
||
627 | case 10: //heading |
||
628 | lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°")); |
||
629 | headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index]))); |
||
630 | break; |
||
631 | case 12: // SPI error |
||
632 | lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index])); |
||
633 | break; |
||
634 | case 14: //i2c error |
||
635 | lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index])); |
||
636 | break; |
||
637 | case 20: //Earthmagnet field |
||
638 | lblNCMF.Invoke((Action)(() => lblNCMF.Text = sAnalogData[index] + "%")); |
||
639 | break; |
||
640 | case 21: //GroundSpeed |
||
641 | lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = ((double)iAnalogData[index] / (double)100).ToString("0.00 m/s"))); |
||
642 | break; |
||
643 | case 28: //Distance East from saved home position -> calculate distance with distance N + height |
||
644 | dTemp = Math.Pow((double)iAnalogData[index], 2) + Math.Pow((double)iAnalogData[index - 1], 2); |
||
645 | dTemp = Math.Sqrt(dTemp) / (double)10; //'flat' distance from HP with N/E |
||
646 | // lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0.00"))); |
||
647 | dTemp = Math.Pow(dTemp, 2) + Math.Pow(((double)iAnalogData[4] / (double)10), 2); //adding 'height' into calculation |
||
2254 | - | 648 | dTemp = Math.Sqrt(dTemp) / (double)10; |
649 | lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = dTemp.ToString("0.0 m"))); |
||
2250 | - | 650 | break; |
651 | case 31: //Sats used |
||
652 | lblNCSat.Invoke((Action)(() => lblNCSat.Text = sAnalogData[index])); |
||
653 | break; |
||
654 | } |
||
655 | } |
||
656 | if (adr == 1) //FC |
||
657 | { |
||
658 | switch (index) |
||
659 | { |
||
660 | case 0: //pitch (German: nick) |
||
661 | artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10))); |
||
662 | lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
||
663 | break; |
||
664 | case 1: //roll |
||
665 | artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10))); |
||
666 | lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
||
667 | break; |
||
668 | case 5: //altitude |
||
669 | lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"))); |
||
670 | break; |
||
671 | case 8: //heading |
||
672 | lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°")); |
||
673 | headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index]))); |
||
674 | break; |
||
675 | case 9: //Voltage |
||
676 | lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V"))); |
||
677 | break; |
||
678 | case 10: //Receiver quality |
||
679 | lblNCRC.Invoke((Action)(() => lblNCRC.Text = sAnalogData[index])); |
||
680 | break; |
||
681 | case 22: // Current |
||
682 | lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"))); |
||
683 | break; |
||
684 | case 23: //capacity used |
||
685 | lblNCCap.Invoke((Action)(() => lblNCCap.Text = (iAnalogData[index]).ToString("0 mAh"))); |
||
686 | break; |
||
687 | case 27: // SPI error |
||
688 | lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index])); |
||
689 | break; |
||
690 | case 28: //i2c error |
||
691 | lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index])); |
||
692 | break; |
||
693 | } |
||
694 | } |
||
695 | index++; |
||
696 | } |
||
697 | } |
||
698 | else |
||
699 | Debug.Print("wrong data-length (66): " + data.Length.ToString()); |
||
700 | } |
||
701 | /// <summary> |
||
702 | /// Version string 'V' |
||
703 | /// </summary> |
||
704 | /// <param name="adr">adress of the active controller (1-FC, 2-NC)</param> |
||
705 | /// <param name="data">the received byte array to process</param> |
||
706 | void _processVersion(byte adr,byte[] data) |
||
707 | { |
||
708 | if (data.Length == 12) |
||
709 | { |
||
710 | if (!check_HWError) |
||
711 | { |
||
712 | string[] sVersionStruct = new string[10] { "SWMajor: ", "SWMinor: ", "ProtoMajor: ", "LabelTextCRC: ", "SWPatch: ", "HardwareError 1: ", "HardwareError 2: ", "HWMajor: ", "BL_Firmware: ", "Flags: " }; |
||
713 | string sVersion = ""; |
||
714 | //sbyte[] signed = Array.ConvertAll(data, b => unchecked((sbyte)b)); |
||
715 | Log(LogMsgType.Warning, (adr == 1 ? "FC-" : "NC-") + "Version: "); |
||
716 | sVersion = "HW V" + (data[7] / 10).ToString() + "." + (data[7] % 10).ToString(); |
||
717 | Log(LogMsgType.Incoming, sVersion); |
||
718 | sVersion = "SW V" + (data[0]).ToString() + "." + (data[1]).ToString() + ((char)(data[4] + 'a')).ToString(); |
||
719 | Log(LogMsgType.Incoming, sVersion); |
||
720 | Log(LogMsgType.Incoming, "BL-Firmware: V" + (data[8] / 100).ToString() + "." + (data[8] % 100).ToString()); |
||
721 | } |
||
722 | if (data[5] > 0) //error0 |
||
723 | { |
||
724 | if (adr == 1) |
||
725 | ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[5].ToString() + ": " + ((FC_HWError0)data[5]).ToString()); |
||
726 | if (adr == 2) |
||
727 | ErrorLog(LogMsgType.Error, "NC - HW-Error " + data[5].ToString() + ": " + ((NC_HWError0)data[5]).ToString()); |
||
728 | } |
||
729 | if (data[6] > 0) //error1 |
||
730 | { |
||
731 | if (adr == 1) |
||
732 | ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[6].ToString() + ": " + ((FC_HWError1)data[6]).ToString()); |
||
733 | if (adr == 2) |
||
734 | ErrorLog(LogMsgType.Error, "NC - Unknown HW-ERROR: " + data[6].ToString()); //@moment NC has only one error field |
||
735 | } |
||
2254 | - | 736 | if((data[5] + data[6] == 0) && _bErrorLog) |
737 | _clearErrorLog(adr==1 ? "FC - HW-Error" : "FC - HW-Error"); |
||
2250 | - | 738 | |
739 | } |
||
740 | check_HWError = false; |
||
741 | } |
||
742 | /// <summary> |
||
743 | /// BL-Ctrl data 'K' |
||
744 | /// for FC you have to use a customized firmware |
||
745 | /// </summary> |
||
746 | /// <param name="data">the received byte array to process</param> |
||
747 | void _processBLCtrl(byte[] data) |
||
748 | { |
||
749 | if (data.Length % 6 == 0) //data.Length up to 96 (16 motors x 6 byte data) --> new datastruct in FC -> not standard! |
||
750 | { |
||
751 | bool bAvailable = false; |
||
752 | for (int i = 0; i < data.Length && data[i] < 8; i += 6) // data[i] < 8 --> at moment there are 8 display fields for motors |
||
753 | { |
||
754 | |||
755 | if ((data[i + 4] & 128) == 128) //Status bit at pos 7 = 128 dec -- if true, motor is available |
||
756 | bAvailable = true; |
||
757 | else |
||
758 | bAvailable = false; |
||
759 | |||
760 | if (data[i] < 4) |
||
761 | { |
||
762 | if (bAvailable) |
||
763 | { |
||
764 | dtMotors1.Rows[data[i]].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A")); |
||
765 | dtMotors1.Rows[data[i]].SetField(2, data[i + 2].ToString("0 °C")); |
||
766 | } |
||
767 | else |
||
768 | { |
||
769 | dtMotors1.Rows[data[i]].SetField(1, "NA"); |
||
770 | dtMotors1.Rows[data[i]].SetField(2, "NA"); |
||
771 | } |
||
772 | } |
||
773 | if (data[i] > 3 && data[i] < 8) |
||
774 | { |
||
775 | if (bAvailable) |
||
776 | { |
||
777 | dtMotors2.Rows[data[i] - 4].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A")); |
||
778 | dtMotors2.Rows[data[i] - 4].SetField(2, data[i + 2].ToString("0 °C")); |
||
779 | } |
||
780 | else |
||
781 | { |
||
782 | dtMotors2.Rows[data[i] - 4].SetField(1, "NA"); |
||
783 | dtMotors2.Rows[data[i] - 4].SetField(2, "NA"); |
||
784 | } |
||
785 | } |
||
786 | } |
||
787 | } |
||
788 | |||
789 | } |
||
790 | /// <summary> |
||
791 | /// Navi-Ctrl data 'O' |
||
792 | /// GPS-Position, capacatiy, flying time... |
||
793 | /// </summary> |
||
794 | /// <param name="data">the received byte array to process</param> |
||
795 | void _processNCData(byte[] data) |
||
796 | { |
||
797 | int i_32, i_16, iVal; |
||
798 | double d; |
||
799 | i_32 = data[4]; |
||
800 | iVal = i_32 << 24; |
||
801 | i_32 = data[3]; |
||
802 | iVal += i_32 << 16; |
||
803 | i_32 = data[2]; |
||
804 | iVal += i_32 << 8; |
||
805 | iVal += data[1]; |
||
806 | d = (double)iVal / Math.Pow(10, 7); |
||
807 | lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = d.ToString("0.######°"))); //GPS-Position: Longitude in decimal degree |
||
808 | //lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = _convertDegree(d))); //GPS-Position: Longitude in minutes, seconds |
||
809 | |||
810 | i_32 = data[8]; |
||
811 | iVal = i_32 << 24; |
||
812 | i_32 = data[7]; |
||
813 | iVal += i_32 << 16; |
||
814 | i_32 = data[6]; |
||
815 | iVal += i_32 << 8; |
||
816 | iVal += data[5]; |
||
817 | d = (double)iVal / Math.Pow(10, 7); |
||
818 | lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = d.ToString("0.######°"))); //GPS-Position: Latitude in decimal degree |
||
2254 | - | 819 | //lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = _convertDegree(d))); //GPS-Position: Latitude in minutes, seconds |
2250 | - | 820 | |
2254 | - | 821 | i_16 = data[28]; |
822 | i_16 = (Int16)(i_16 << 8); |
||
823 | iVal = data[27] + i_16; |
||
824 | lblNCDistWP.Invoke((Action)(() => lblNCDistWP.Text = ((double)iVal/ (double)10).ToString("0.0 m"))); //Distance to next WP |
||
825 | |||
826 | i_16 = data[45]; |
||
827 | i_16 = (Int16)(i_16 << 8); |
||
828 | iVal = data[44] + i_16; |
||
829 | lblNCDistHP1.Invoke((Action)(() => lblNCDistHP1.Text = ((double)iVal/ (double)10).ToString("0.0 m"))); //Distance to next WP |
||
830 | |||
831 | lblNCWPIndex.Invoke((Action)(() => lblNCWPIndex.Text = data[48].ToString())); //Waypoint index |
||
832 | lblNCWPCount.Invoke((Action)(() => lblNCWPCount.Text = data[49].ToString())); //Waypoints count |
||
833 | |||
2250 | - | 834 | i_16 = data[81]; |
835 | i_16 = (Int16)(i_16 << 8); |
||
836 | iVal = data[80] + i_16; |
||
837 | lblNCCap.Invoke((Action)(() => lblNCCap.Text = iVal.ToString() + " mAh")); //Capacity used |
||
838 | |||
839 | i_16 = data[56]; |
||
840 | i_16 = (Int16)(i_16 << 8); |
||
841 | iVal = data[55] + i_16; |
||
842 | TimeSpan t = TimeSpan.FromSeconds(iVal); |
||
843 | string Text = t.Hours.ToString("D2") + ":" + t.Minutes.ToString("D2") + ":" + t.Seconds.ToString("D2"); |
||
844 | lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = Text.ToString())); //Flying time |
||
845 | |||
846 | lblNCRC.Invoke((Action)(() => lblNCRC.Text = data[66].ToString())); //RC quality |
||
847 | lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = data[69].ToString())); //NC Errornumber |
||
848 | //if (data[69] > 0) |
||
849 | // _readNCError(); |
||
850 | //break; |
||
851 | if (data[69] > 0 & data[69] < 44) |
||
852 | ErrorLog(LogMsgType.Error, "NC Error [" + data[69].ToString() + "]: " + NC_Error[data[69]]); |
||
2254 | - | 853 | else |
854 | if(_bErrorLog) _clearErrorLog("NC Error"); |
||
2250 | - | 855 | |
856 | } |
||
857 | /// <summary> |
||
2257 | - | 858 | /// Navi-Ctrl WP data struct 'X' |
859 | /// called by index |
||
860 | /// </summary> |
||
861 | /// <param name="data">the received byte array to process</param> |
||
862 | void _processWPData(byte[] data) |
||
863 | { |
||
864 | if (data.Length >= 28) |
||
865 | { |
||
866 | int count = data[0]; |
||
867 | int index = data[1]; |
||
868 | cbWPIndex.Invoke((Action)(() => cbWPIndex.Items.Clear())); |
||
869 | for (int i = 0; i < count; i++) |
||
870 | cbWPIndex.Invoke((Action)(() => cbWPIndex.Items.Add(i + 1))); |
||
871 | cbWPIndex.Invoke((Action)(() => cbWPIndex.SelectedItem = index)); |
||
872 | DataRow dr = dtWaypoints.NewRow(); |
||
873 | dr = Waypoints.toDataRow(data, dr); |
||
874 | dtWaypoints.Rows.Add(dr); |
||
875 | dgvWP.Invoke((Action)(() => dgvWP.Update())); |
||
876 | } |
||
877 | else |
||
878 | Debug.Print(new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length))); |
||
879 | } |
||
880 | /// <summary> |
||
2250 | - | 881 | /// OSD Menue 'L' |
882 | /// single page called by pagenumber |
||
883 | /// no autoupdate |
||
884 | /// </summary> |
||
885 | /// <param name="data">the received byte array to process</param> |
||
886 | void _processOSDSingle(byte[] data) |
||
887 | { |
||
888 | if (data.Length == 84) |
||
889 | { |
||
890 | string sMessage = ""; |
||
891 | iOSDPage = data[0]; |
||
892 | iOSDMax = data[1]; |
||
893 | if (cbOSD.Items.Count != iOSDMax) _initOSDCB(); |
||
894 | sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 2, data.Length - 4)); |
||
895 | OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)); |
||
896 | OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)); |
||
897 | OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)); |
||
898 | OSD(LogMsgType.Incoming, sMessage.Substring(60, 20)); |
||
899 | lblOSDPageNr.Invoke((Action)(() => lblOSDPageNr.Text = iOSDPage.ToString("[0]"))); |
||
900 | |||
901 | } |
||
902 | else |
||
903 | OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 84)"); |
||
904 | |||
905 | } |
||
906 | /// <summary> |
||
907 | /// OSD Menue 'H' |
||
908 | /// called by keys (0x01,0x02,0x03,0x04) |
||
909 | /// autoupdate |
||
910 | /// </summary> |
||
911 | /// <param name="data">the received byte array to process</param> |
||
912 | void _processOSDAuto(byte[] data) |
||
913 | { |
||
914 | if (data.Length == 81) |
||
915 | { |
||
916 | string sMessage = ""; |
||
917 | sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1)); |
||
918 | OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)); |
||
919 | OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)); |
||
920 | OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)); |
||
921 | OSD(LogMsgType.Incoming, sMessage.Substring(60, 20)); |
||
922 | |||
923 | } |
||
924 | else |
||
925 | OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)"); |
||
926 | } |
||
927 | #endregion processing received data |
||
928 | |||
2233 | - | 929 | /// <summary> send message to controller to request data |
930 | /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/ |
||
931 | /// </summary> |
||
932 | /// <param name="CMDID"> the command ID </param> |
||
933 | /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param> |
||
934 | private void _sendControllerMessage(char CMDID, byte address) |
||
935 | { |
||
936 | if (simpleSerialPort.Port.IsOpen) |
||
937 | { |
||
938 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
939 | byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address); |
||
940 | serialStream.Write(bytes, 0, bytes.Length); |
||
941 | |||
942 | } |
||
943 | else |
||
944 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
945 | } |
||
946 | /// <summary> send message to controller to request data |
||
947 | /// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/ |
||
948 | /// </summary> |
||
949 | /// <param name="CMDID"> the command ID </param> |
||
950 | /// <param name="address"> the address of the controller: 0-any, 1-FC, 2-NC </param> |
||
951 | /// <param name="data"> additional data for the request</param> |
||
952 | private void _sendControllerMessage(char CMDID, byte address, byte[]data) |
||
953 | { |
||
954 | if (simpleSerialPort.Port.IsOpen) |
||
955 | { |
||
956 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
957 | byte[] bytes = FlightControllerMessage.CreateMessage(CMDID, address,data); |
||
958 | serialStream.Write(bytes, 0, bytes.Length); |
||
959 | |||
960 | } |
||
961 | else |
||
962 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
963 | } |
||
964 | |||
965 | /// <summary> |
||
966 | /// read the analog-label names for the actual controller |
||
967 | /// and load it into listbox |
||
968 | /// </summary> |
||
969 | void _loadLabelNames() |
||
970 | { |
||
971 | if (_iCtrlAct > 0 && _iCtrlAct < 3) |
||
972 | { |
||
973 | switch (_iCtrlAct) |
||
974 | { |
||
975 | case 1: |
||
976 | sAnalogLabel = Properties.Resources.FCLabelTexts.Split(new[] { Environment.NewLine }, StringSplitOptions.None); |
||
977 | break; |
||
978 | case 2: |
||
979 | sAnalogLabel = Properties.Resources.NCLabelTexts.Split(new[] { Environment.NewLine }, StringSplitOptions.None); |
||
980 | break; |
||
981 | } |
||
982 | for (int i = 0; i < 32; i++) |
||
983 | { |
||
984 | if (dtAnalog.Rows.Count < 32) |
||
985 | dtAnalog.Rows.Add(sAnalogLabel[i], ""); |
||
986 | else |
||
987 | dtAnalog.Rows[i].SetField(0, sAnalogLabel[i]); |
||
988 | } |
||
989 | dataGridView1.Invoke((Action)(()=>dataGridView1.Refresh())); |
||
990 | } |
||
991 | } |
||
992 | /// <summary> |
||
993 | /// no longer used... |
||
994 | /// read the analog-label textfile for the actual controller |
||
995 | /// </summary> |
||
996 | private void _loadLabelFile() |
||
997 | { |
||
998 | switch (_iCtrlAct) |
||
999 | { |
||
1000 | case 1: |
||
1001 | fileName = "FCLabelTexts.txt"; |
||
1002 | break; |
||
1003 | case 2: |
||
1004 | fileName = "NCLabelTexts.txt"; |
||
1005 | break; |
||
1006 | //default: |
||
1007 | // fileName = "NCLabelTexts.txt"; |
||
1008 | // break; |
||
1009 | } |
||
1010 | |||
1011 | if (File.Exists(filePath + "\\" + fileName)) |
||
1012 | { |
||
1013 | sAnalogLabel.Initialize(); |
||
1014 | sAnalogLabel = File.ReadAllLines(filePath + "\\" + fileName); |
||
1015 | lbLabels.Invoke((Action)(() => lbLabels.Items.Clear())); |
||
1016 | lbLabels.Invoke((Action)(() => lbLabels.Update())); |
||
1017 | lbLabels.Invoke((Action)(() => lbLabels.Items.AddRange(sAnalogLabel))); |
||
1018 | Console.WriteLine("Names loaded from file"); |
||
1019 | lblFileName.Invoke((Action)(() => lblFileName.Text = fileName)); |
||
1020 | } |
||
1021 | else |
||
1022 | { |
||
1023 | _readCont(false); |
||
1024 | Log(LogMsgType.Error, "Label-file not found!"); |
||
1025 | Log(LogMsgType.Error, "Please go to settings-tab and load the label texts from the copter control (FC & NC)"); |
||
1026 | Log(LogMsgType.Error, "When done, you have to save the label texts with the 'save' button!"); |
||
1027 | } |
||
1028 | } |
||
1029 | /// <summary> |
||
1030 | /// no longer used... |
||
1031 | /// assign the analog-label names from the textfile to the datatable |
||
1032 | /// |
||
1033 | /// </summary> |
||
1034 | private void _assignLabelNames() |
||
1035 | { |
||
1036 | if (lbLabels.Items.Count == 32) |
||
1037 | { |
||
1038 | lbLabels.Items.CopyTo(sAnalogLabel, 0); |
||
1039 | for (int i = 0; i < 32; i++) |
||
1040 | { |
||
1041 | if (dtAnalog.Rows.Count < 32) |
||
1042 | dtAnalog.Rows.Add(sAnalogLabel[i], ""); |
||
1043 | else |
||
1044 | dtAnalog.Rows[i].SetField(0, sAnalogLabel[i]); |
||
1045 | |||
1046 | } |
||
1047 | } |
||
1048 | } |
||
1049 | /// <summary> |
||
1050 | /// get the version struct of actual controller |
||
1051 | /// </summary> |
||
1052 | /// <summary> |
||
1053 | /// get the labeltexts for the analog values |
||
1054 | /// </summary> |
||
1055 | private void _getAnalogLabels() |
||
1056 | { |
||
1057 | if (simpleSerialPort.Port.IsOpen) |
||
1058 | { |
||
1059 | iLableIndex = 0; |
||
1060 | for (int i = 0; i < 32; i++) |
||
1061 | { |
||
1062 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
1063 | byte[] bytes = FlightControllerMessage.CreateMessage('a', 0, new byte[1] { (byte)i }); |
||
1064 | serialStream.Write(bytes, 0, bytes.Length); |
||
1065 | Thread.Sleep(10); |
||
1066 | } |
||
1067 | } |
||
1068 | else |
||
1069 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
1070 | } |
||
1071 | /// <summary> |
||
1072 | /// get the labeltext for a single label |
||
1073 | /// </summary> |
||
1074 | /// <param name="iIndex">index of the label</param> |
||
1075 | private void _getAnalogLabels(int iIndex) |
||
1076 | { |
||
1077 | if (simpleSerialPort.Port.IsOpen) |
||
1078 | { |
||
1079 | if (iIndex < 32) |
||
1080 | { |
||
1081 | iLableIndex = iIndex; |
||
1082 | _sendControllerMessage('a', 0, new byte[1] { (byte)iLableIndex }); |
||
1083 | } |
||
1084 | } |
||
1085 | else |
||
1086 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
1087 | } |
||
1088 | private void _getVersion() |
||
1089 | { |
||
1090 | _sendControllerMessage('v', 0); |
||
1091 | } |
||
1092 | /// <summary> |
||
1093 | /// get FC version struct via NC |
||
1094 | /// by sending '1' as data (not documented in wiki...) |
||
1095 | /// returns HW error 255 (comment in uart1.c : tells the KopterTool that it is the FC-version) |
||
1096 | /// </summary> |
||
1097 | /// <param name="ctrl">controller number 1=FC</param> |
||
1098 | private void _getVersion(byte ctrl) |
||
1099 | { |
||
1100 | _sendControllerMessage('v', 0, new byte[1] {ctrl}); |
||
1101 | } |
||
1102 | /// <summary> |
||
1103 | /// Switch back to NC by sending the 'Magic Packet' 0x1B,0x1B,0x55,0xAA,0x00 |
||
1104 | /// </summary> |
||
1105 | private void _switchToNC() |
||
1106 | { |
||
1107 | if (simpleSerialPort.Port.IsOpen) |
||
1108 | { |
||
1109 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
1110 | byte[] bytes = new byte[5] { 0x1B,0x1B,0x55,0xAA,0x00 }; |
||
1111 | serialStream.Write(bytes, 0, bytes.Length); |
||
1112 | |||
1113 | Thread.Sleep(100); |
||
1114 | _getVersion(); |
||
1115 | Thread.Sleep(100); |
||
1116 | _OSDMenue(0); |
||
1117 | } |
||
1118 | else |
||
1119 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
1120 | } |
||
1121 | /// <summary> |
||
1122 | /// switch to FC |
||
1123 | /// </summary> |
||
1124 | private void _switchToFC() |
||
1125 | { |
||
1126 | _sendControllerMessage('u', 2, new byte[1] { (byte)0 }); |
||
1127 | Thread.Sleep(100); |
||
1128 | _getVersion(); |
||
1129 | Thread.Sleep(100); |
||
1130 | _OSDMenue(0); |
||
1131 | } |
||
1132 | /// <summary> |
||
1133 | /// send RESET signal to FC |
||
1134 | /// </summary> |
||
1135 | private void _resetCtrl() |
||
1136 | { |
||
1137 | _sendControllerMessage('R', 1); |
||
1138 | } |
||
1139 | /// <summary> |
||
1140 | /// poll the debug data (4sec subscription) |
||
1141 | /// </summary> |
||
1142 | /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param> |
||
1143 | private void _readDebugData(bool auto) |
||
1144 | { |
||
1145 | byte interval = auto ? debugInterval : (byte)0; |
||
1146 | _sendControllerMessage('d', 0, new byte[1] { debugInterval }); |
||
1147 | } |
||
1148 | /// <summary> |
||
1149 | /// poll the BL-CTRL status via NC (4sec subscription) |
||
1150 | /// </summary> |
||
1151 | /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param> |
||
1152 | private void _readBLCtrl(bool auto) |
||
1153 | { |
||
1154 | byte interval = auto ? blctrlInterval : (byte)0; |
||
1155 | _sendControllerMessage('k', 0, new byte[1] { interval }); |
||
1156 | } |
||
1157 | /// <summary> |
||
1158 | /// poll the NC data struct (4sec subscription) |
||
1159 | /// </summary> |
||
1160 | /// <param name="auto"> onetimequery(false) or autoupdate(true) with set timing interval </param> |
||
1161 | private void _readNavData(bool auto) |
||
1162 | { |
||
1163 | byte interval = auto ? navctrlInterval : (byte)0; |
||
1164 | _sendControllerMessage('o', 2, new byte[1] { interval }); |
||
1165 | } |
||
1166 | /// <summary> |
||
1167 | /// get the errortext for pending NC error |
||
1168 | /// </summary> |
||
1169 | private void _readNCError() |
||
1170 | { |
||
1171 | _sendControllerMessage('e', 2); |
||
1172 | } |
||
1173 | /// <summary> |
||
1174 | /// start/stop continous polling of controller values |
||
1175 | /// </summary> |
||
1176 | /// <param name="b">start/stop switch</param> |
||
1177 | void _readCont(bool b) |
||
1178 | { |
||
1179 | bReadContinously = b; |
||
1180 | btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.Text = bReadContinously ? "stop automatic" + Environment.NewLine + "data refresh" : "start automatic" + Environment.NewLine + "data refresh")); |
||
1181 | btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.BackColor = bReadContinously ? Color.FromArgb(192, 255, 192) : Color.FromArgb(224, 224, 224))); |
||
1182 | if (bReadContinously) |
||
1183 | { |
||
2250 | - | 1184 | if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); } |
1185 | if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); } |
||
1186 | if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); } |
||
1187 | if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); Thread.Sleep(10);} |
||
2233 | - | 1188 | lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0))); |
1189 | } |
||
1190 | else |
||
1191 | lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(224, 224, 224))); |
||
1192 | _iLifeCounter = 0; |
||
1193 | } |
||
1194 | /// <summary> |
||
2250 | - | 1195 | /// set values to "NA" when not available with FC |
2233 | - | 1196 | /// </summary> |
1197 | void _setFieldsNA() |
||
1198 | { |
||
1199 | Thread.Sleep(100); |
||
2250 | - | 1200 | _initDTMotors(); |
2233 | - | 1201 | lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = "NA")); //FlightTime |
2250 | - | 1202 | lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = "NA")); //NC ErrorNr |
1203 | lblNCMF.Invoke((Action)(() => lblNCMF.Text = "NA")); //earth magnet field |
||
1204 | lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = "NA")); //GroundSpeed |
||
1205 | lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = "NA")); //Distance to HP |
||
1206 | lblNCSat.Invoke((Action)(() => lblNCSat.Text = "NA")); //Sats used |
||
1207 | lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = "NA")); //GPS position - longitude |
||
1208 | lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = "NA")); //GPS position - latitude |
||
2254 | - | 1209 | lblNCDistWP.Invoke((Action)(() => lblNCDistWP.Text = "NA")); //next WP distance |
1210 | lblNCWPIndex.Invoke((Action)(() => lblNCWPIndex.Text = "NA")); //index of actual WP |
||
1211 | lblNCWPCount.Invoke((Action)(() => lblNCWPCount.Text = "NA")); //count of items in WP list |
||
2233 | - | 1212 | } |
1213 | /// <summary> |
||
1214 | /// one time query of the OSD Menue with pagenumber |
||
1215 | /// </summary> |
||
1216 | /// <param name="iMenue">Menue page</param> |
||
1217 | void _OSDMenue(int iMenue) |
||
1218 | { |
||
1219 | if (simpleSerialPort.Port.IsOpen) |
||
1220 | { |
||
1221 | if (iMenue > iOSDMax) |
||
1222 | iMenue = 0; |
||
1223 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
1224 | byte[] bytes = FlightControllerMessage.CreateMessage('l', 0, new byte[1] { (byte)iMenue }); |
||
1225 | serialStream.Write(bytes, 0, bytes.Length); |
||
1226 | } |
||
1227 | else |
||
1228 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
1229 | |||
1230 | } |
||
1231 | /// <summary> |
||
1232 | /// call the OSDMenue and start autorefresh |
||
1233 | /// usually by sending a menuekey |
||
1234 | /// a bit tricky - but by sending inverted value of 32 (32 = 0010 0000) you can start the OSD menue with autoupdate (abo) without switching the page with the keyvalues (0x1, 0x2) |
||
1235 | /// therefore the value has to be negative (inverted) in order to distinguish from old (2 line) menuestyle |
||
1236 | /// and must not have any bits of the menue keys 0x1 0x2 0x4 0x8 (0x10?) --> 0x20 = -33 |
||
1237 | /// </summary> |
||
1238 | void _OSDMenueAutoRefresh() |
||
1239 | { |
||
1240 | _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)(-33)),OSDInterval }); |
||
1241 | } |
||
1242 | void _OSDMenueAutoRefresh(byte key) |
||
1243 | { |
||
1244 | _sendControllerMessage('h', 0, new byte[2] { unchecked((byte)~key), OSDInterval }); |
||
1245 | } |
||
1246 | /// <summary> |
||
1247 | /// initialize the OSD menue combobox |
||
1248 | /// combox is filled by numbers from 0 to max pagenumber |
||
1249 | /// </summary> |
||
1250 | void _initOSDCB() |
||
1251 | { |
||
1252 | _bCBInit = true; |
||
1253 | if(iOSDMax == 0) |
||
1254 | { |
||
1255 | _OSDMenue(0); |
||
1256 | Thread.Sleep(10); |
||
1257 | } |
||
1258 | cbOSD.Invoke((Action)(()=>cbOSD.Items.Clear())); |
||
1259 | for(int i = 0; i <= iOSDMax;i++) |
||
1260 | { |
||
1261 | cbOSD.Invoke((Action)(() => cbOSD.Items.Add(i))); |
||
1262 | } |
||
1263 | cbOSD.Invoke((Action)(() => cbOSD.SelectedItem = iOSDPage)); |
||
1264 | _bCBInit = false; |
||
1265 | } |
||
1266 | void _readIni() |
||
1267 | { |
||
1268 | if (!File.Exists(filePath + "\\MKLiveViewSettings.ini")) |
||
1269 | _writeIni(); |
||
1270 | IniFile ini = new IniFile("MKLiveViewSettings.ini"); |
||
1271 | ini.path = filePath + "\\MKLiveViewSettings.ini"; |
||
1272 | |||
1273 | string sVal = ini.IniReadValue("default", "AutorefreshDebugData"); |
||
1274 | _debugDataAutorefresh = Convert.ToBoolean(sVal); |
||
1275 | sVal = ini.IniReadValue("default", "AutorefreshNavCtrlData"); |
||
1276 | _navCtrlDataAutorefresh = Convert.ToBoolean(sVal); |
||
1277 | sVal = ini.IniReadValue("default", "AutorefreshBLCtrlData"); |
||
1278 | _blctrlDataAutorefresh = Convert.ToBoolean(sVal); |
||
1279 | sVal = ini.IniReadValue("default", "AutorefreshOSDData"); |
||
1280 | _OSDAutorefresh = Convert.ToBoolean(sVal); |
||
1281 | |||
1282 | sVal = ini.IniReadValue("default", "IntervalDebugData"); |
||
1283 | debugInterval = (byte)Convert.ToInt16(sVal); |
||
1284 | sVal = ini.IniReadValue("default", "IntervalNavCtrlData"); |
||
1285 | navctrlInterval = (byte)Convert.ToInt16(sVal); |
||
1286 | sVal = ini.IniReadValue("default", "IntervalBLCtrlData"); |
||
1287 | blctrlInterval = (byte)Convert.ToInt16(sVal); |
||
1288 | sVal = ini.IniReadValue("default", "IntervalOSDData"); |
||
1289 | OSDInterval = (byte)Convert.ToInt16(sVal); |
||
2257 | - | 1290 | for(int i = 0; i < 12; i++) |
1291 | { |
||
1292 | sVal = ini.IniReadValue("serial", "ch" + i.ToString() + "Val"); |
||
1293 | if(sVal != "") |
||
1294 | serChan[i] = Convert.ToInt16(sVal); |
||
1295 | sVal = ini.IniReadValue("serial", "ch" + i.ToString() + "Title"); |
||
1296 | if(sVal != "") |
||
1297 | serChanTitle[i] = sVal; |
||
1298 | } |
||
2233 | - | 1299 | } |
1300 | void _writeIni() |
||
1301 | { |
||
1302 | |||
1303 | IniFile ini = new IniFile("MKLiveViewSettings.ini"); |
||
1304 | ini.path = filePath + "\\MKLiveViewSettings.ini"; |
||
1305 | |||
1306 | ini.IniWriteValue("default", "AutorefreshDebugData", _debugDataAutorefresh ? "true":"false"); |
||
1307 | ini.IniWriteValue("default", "AutorefreshNavCtrlData", _navCtrlDataAutorefresh ? "true":"false"); |
||
1308 | ini.IniWriteValue("default", "AutorefreshBLCtrlData", _blctrlDataAutorefresh ? "true":"false"); |
||
1309 | ini.IniWriteValue("default", "AutorefreshOSDData", _OSDAutorefresh ? "true":"false"); |
||
1310 | |||
1311 | ini.IniWriteValue("default", "IntervalDebugData", debugInterval.ToString()); |
||
1312 | ini.IniWriteValue("default", "IntervalNavCtrlData", navctrlInterval.ToString()); |
||
1313 | ini.IniWriteValue("default", "IntervalBLCtrlData", blctrlInterval.ToString()); |
||
1314 | ini.IniWriteValue("default", "IntervalOSDData", OSDInterval.ToString()); |
||
2257 | - | 1315 | |
1316 | for (int i = 0; i < 12; i++) |
||
1317 | { |
||
1318 | ini.IniWriteValue("serial", "ch" + i.ToString() + "Val", serChan[i].ToString()); |
||
1319 | ini.IniWriteValue("serial", "ch" + i.ToString() + "Title", serChanTitle[i]); |
||
1320 | } |
||
1321 | |||
2233 | - | 1322 | } |
1323 | |||
2250 | - | 1324 | /// <summary> |
1325 | /// initialize the 2 datatables for motor values |
||
1326 | /// dtMotors1 - motor 1 - 4 |
||
1327 | /// dtMotors2 - motor 5 - 8 |
||
1328 | /// DataGridView dgvMotors1/2 are bound to dtMotors1/2 |
||
1329 | /// </summary> |
||
1330 | void _initDTMotors() |
||
1331 | { |
||
1332 | for(int i = 0; i < 4; i++) |
||
1333 | { |
||
1334 | if (dtMotors1.Rows.Count < 4) |
||
1335 | dtMotors1.Rows.Add((i + 1).ToString(), "NA", "NA"); |
||
1336 | else |
||
1337 | { |
||
1338 | dtMotors1.Rows[i].SetField(1, "NA"); |
||
1339 | dtMotors1.Rows[i].SetField(2, "NA"); |
||
1340 | } |
||
1341 | if (dtMotors2.Rows.Count < 4) |
||
1342 | dtMotors2.Rows.Add((i + 5).ToString(), "NA", "NA"); |
||
1343 | else |
||
1344 | { |
||
1345 | dtMotors2.Rows[i].SetField(1, "NA"); |
||
1346 | dtMotors2.Rows[i].SetField(2, "NA"); |
||
1347 | } |
||
1348 | } |
||
1349 | dgvMotors1.Invoke((Action)(() => dgvMotors1.Refresh())); |
||
1350 | dgvMotors2.Invoke((Action)(() => dgvMotors2.Refresh())); |
||
1351 | } |
||
1352 | |||
1353 | /// <summary> |
||
1354 | /// Convert decimal degrees to degrees, minutes, seconds, milliseconds |
||
1355 | /// </summary> |
||
1356 | /// <param name="coord">the degree value as double</param> |
||
1357 | /// <returns>0° 0' 0,0"</returns> |
||
1358 | string _convertDegree(double coord) |
||
1359 | { |
||
1360 | //double minutes = (degree - Math.Floor(degree)) * 60.0; |
||
1361 | //double seconds = (minutes - Math.Floor(minutes)) * 60.0; |
||
1362 | //double tenths = (seconds - Math.Floor(seconds)) * 10.0; |
||
1363 | //// get rid of fractional part |
||
1364 | //minutes = Math.Floor(minutes); |
||
1365 | //seconds = Math.Floor(seconds); |
||
1366 | //tenths = Math.Floor(tenths); |
||
1367 | |||
1368 | |||
1369 | //int sec = (int)Math.Round(coord * 3600); |
||
1370 | //int deg = sec / 3600; |
||
1371 | //sec = Math.Abs(sec % 3600); |
||
1372 | //int min = sec / 60; |
||
1373 | //sec %= 60; |
||
1374 | |||
1375 | var ts = TimeSpan.FromHours(Math.Abs(coord)); |
||
1376 | double deg = Math.Sign(coord) * Math.Floor(ts.TotalHours); |
||
1377 | int min = ts.Minutes; |
||
1378 | int sec = ts.Seconds; |
||
1379 | int milli = ts.Milliseconds; |
||
1380 | |||
1381 | return deg.ToString("0° ") + min.ToString("0") + "' " + sec.ToString("0") + "," + milli.ToString() + "\""; |
||
1382 | } |
||
2257 | - | 1383 | /// <summary> |
1384 | /// Clear the line in the errorlog window |
||
1385 | /// containing the error string when error has ceased |
||
1386 | /// </summary> |
||
1387 | /// <param name="s">substring of errrormessage</param> |
||
2254 | - | 1388 | void _clearErrorLog(string s) |
1389 | { |
||
1390 | rtfError.Invoke((Action)(() => |
||
1391 | { |
||
1392 | if (rtfError.Text.Contains(s)) |
||
1393 | { |
||
1394 | int iLength = 0; |
||
1395 | int iStart = rtfError.Text.IndexOf(s); |
||
1396 | int iEnd = rtfError.Text.IndexOf('\n', iStart); |
||
1397 | if (iEnd > 0) |
||
1398 | { |
||
1399 | iLength = iEnd + 1; |
||
1400 | int iHttp = rtfError.Text.IndexOf("http", iEnd); |
||
1401 | if (iHttp == iLength) |
||
1402 | { |
||
1403 | int iEnd2 = rtfError.Text.IndexOf('\n', iLength); |
||
1404 | if (iEnd2 > iLength) |
||
1405 | { |
||
1406 | iLength = iEnd2 + 1; |
||
1407 | rtfError.Select(iStart, iLength); |
||
1408 | rtfError.SelectedText = string.Empty; |
||
1409 | if(rtfError.Text.Length < 2) _bErrorLog = false; |
||
1410 | } |
||
1411 | |||
1412 | } |
||
1413 | else |
||
1414 | { |
||
1415 | rtfError.Select(iStart, iLength); |
||
1416 | rtfError.SelectedText = string.Empty; |
||
1417 | if(rtfError.Text.Length < 2) _bErrorLog = false; |
||
1418 | } |
||
1419 | } |
||
1420 | } |
||
1421 | })); |
||
1422 | |||
1423 | } |
||
2257 | - | 1424 | /// <summary> |
1425 | /// request the Waypoint at index |
||
1426 | /// </summary> |
||
1427 | /// <param name="index"></param> |
||
1428 | void _getpWP(int index) |
||
1429 | { |
||
1430 | if (simpleSerialPort.Port.IsOpen) |
||
1431 | { |
||
1432 | Stream serialStream = simpleSerialPort.Port.BaseStream; |
||
1433 | byte[] bytes = FlightControllerMessage.CreateMessage('x', 2, new byte[1] { (byte)index }); |
||
1434 | serialStream.Write(bytes, 0, bytes.Length); |
||
1435 | } |
||
1436 | else |
||
1437 | Log(LogMsgType.Error, "NOT CONNECTED!"); |
||
1438 | |||
1439 | } |
||
1440 | /// <summary> |
||
1441 | /// Sending the serial channel values |
||
1442 | /// </summary> |
||
1443 | void _sendSerialData() |
||
1444 | { |
||
1445 | byte[] serData = new byte[12]; |
||
1446 | for(int i = 0; i < 12; i++) |
||
1447 | { |
||
1448 | serData[i] = unchecked((byte)(serChan[i] - 127)); |
||
1449 | } |
||
1450 | _sendControllerMessage('y', 1, serData); |
||
1451 | } |
||
1452 | |||
1453 | void _initSerialCtrl() |
||
1454 | { |
||
1455 | trckbarSerial1.Value = serChan[0]; |
||
1456 | textBoxSerial1.Text = serChanTitle[0]; |
||
1457 | lblTbSerial1.Text = serChan[0].ToString(); |
||
1458 | trckbarSerial2.Value = serChan[1]; |
||
1459 | textBoxSerial2.Text = serChanTitle[1]; |
||
1460 | lblTbSerial2.Text = serChan[1].ToString(); |
||
1461 | } |
||
2233 | - | 1462 | #endregion functions |
1463 | |||
1464 | #region buttons |
||
1465 | private void buttonReset_Click(object sender, EventArgs e) |
||
1466 | { |
||
1467 | _resetCtrl(); |
||
1468 | } |
||
1469 | private void btnVersion_Click(object sender, EventArgs e) |
||
1470 | { |
||
1471 | _getVersion(); |
||
1472 | } |
||
1473 | private void btnAnalogLabels_Click(object sender, EventArgs e) |
||
1474 | { |
||
1475 | _getAnalogLabels(0); |
||
1476 | } |
||
1477 | private void btnDbgData_Click(object sender, EventArgs e) |
||
1478 | { |
||
1479 | _readDebugData(false); //onetime reading of debug data --> subscription lasts 4sec - this means you will receive data for 4 seconds |
||
1480 | } |
||
1481 | private void btnSaveLabels_Click(object sender, EventArgs e) |
||
1482 | { |
||
1483 | switch (_iCtrlAct) |
||
1484 | { |
||
1485 | case 1: |
||
1486 | fileName = "FCLabelTexts.txt"; |
||
1487 | break; |
||
1488 | case 2: |
||
1489 | fileName = "NCLabelTexts.txt"; |
||
1490 | break; |
||
1491 | default: |
||
1492 | fileName = "NCLabelTexts.txt"; |
||
1493 | break; |
||
1494 | } |
||
1495 | if (sAnalogLabel[0] != null) |
||
1496 | { |
||
1497 | File.WriteAllLines(filePath + "\\" + fileName, sAnalogLabel); |
||
1498 | Console.WriteLine("Names saved to file"); |
||
1499 | _loadLabelFile(); |
||
1500 | } |
||
1501 | else |
||
1502 | Log(LogMsgType.Warning, "there's no data -> read first from fc/nc!"); |
||
1503 | } |
||
1504 | private void btnLoadLabels_Click(object sender, EventArgs e) |
||
1505 | { |
||
1506 | _assignLabelNames(); |
||
1507 | } |
||
1508 | private void btnReadLabelFile_Click(object sender, EventArgs e) |
||
1509 | { |
||
1510 | _loadLabelFile(); |
||
1511 | } |
||
1512 | private void btnSwitchFC_Click(object sender, EventArgs e) |
||
1513 | { |
||
1514 | _switchToFC(); |
||
1515 | } |
||
1516 | private void btnSwitchNC_Click(object sender, EventArgs e) |
||
1517 | { |
||
1518 | _switchToNC(); |
||
1519 | } |
||
1520 | private void btnReadDbgCont_Click(object sender, EventArgs e) |
||
1521 | { |
||
1522 | _readCont(!bReadContinously); |
||
1523 | } |
||
1524 | private void btnReadBLCtrl_Click(object sender, EventArgs e) |
||
1525 | { |
||
1526 | |||
1527 | if (_iCtrlAct == 2) _readBLCtrl(false); |
||
1528 | else Log(LogMsgType.Warning, "only available when connected to NC"); |
||
1529 | } |
||
1530 | private void btnGetNaviData_Click(object sender, EventArgs e) |
||
1531 | { |
||
1532 | if (_iCtrlAct == 2) _readNavData(false); |
||
1533 | else Log(LogMsgType.Warning, "only available when connected to NC"); |
||
1534 | } |
||
1535 | private void btnConn_Click(object sender, EventArgs e) |
||
1536 | { |
||
1537 | simpleSerialPort.Connect(!simpleSerialPort.Port.IsOpen); |
||
1538 | } |
||
1539 | private void button3_Click(object sender, EventArgs e) |
||
1540 | { |
||
1541 | _getVersion(1); |
||
1542 | } |
||
1543 | private void button4_Click(object sender, EventArgs e) |
||
1544 | { |
||
1545 | _getVersion(2); |
||
1546 | } |
||
1547 | private void btnOSD_Click(object sender, EventArgs e) |
||
1548 | { |
||
1549 | if (iOSDPage > iOSDMax) |
||
1550 | iOSDPage = 0; |
||
1551 | _OSDMenue(iOSDPage); |
||
1552 | } |
||
1553 | private void btnOSDForward_Click(object sender, EventArgs e) |
||
1554 | { |
||
1555 | iOSDPage++; |
||
1556 | if (iOSDPage > iOSDMax) |
||
1557 | iOSDPage = 0; |
||
1558 | |||
1559 | _OSDMenue(iOSDPage); |
||
1560 | } |
||
1561 | private void btnOSDBackward_Click(object sender, EventArgs e) |
||
1562 | { |
||
1563 | iOSDPage--; |
||
1564 | if (iOSDPage < 0) |
||
1565 | iOSDPage = iOSDMax; |
||
1566 | |||
1567 | _OSDMenue(iOSDPage); |
||
1568 | } |
||
1569 | private void btnOSDAuto_Click(object sender, EventArgs e) |
||
1570 | { |
||
1571 | _OSDMenueAutoRefresh(); |
||
1572 | } |
||
1573 | /// call the OSDMenue with Key 0x8 |
||
1574 | private void btnOSDLeave_Click(object sender, EventArgs e) |
||
1575 | { |
||
1576 | _OSDMenueAutoRefresh(8); |
||
1577 | } |
||
1578 | /// call the OSDMenue with Key 0x4 |
||
1579 | private void btnOSDEnter_Click(object sender, EventArgs e) |
||
1580 | { |
||
1581 | _OSDMenueAutoRefresh(4); |
||
1582 | } |
||
2257 | - | 1583 | private void btnGetWP_Click(object sender, EventArgs e) |
1584 | { |
||
1585 | if(cbWPIndex.Items.Count >0) |
||
1586 | _getpWP((int)cbWPIndex.SelectedItem); |
||
1587 | else |
||
1588 | _getpWP(1); |
||
1589 | } |
||
2233 | - | 1590 | #endregion buttons |
2254 | - | 1591 | |
2257 | - | 1592 | private void tbSerial1_Scroll(object sender, EventArgs e) |
1593 | { |
||
1594 | lblTbSerial1.Text = trckbarSerial1.Value.ToString(); |
||
1595 | serChan[0] = trckbarSerial1.Value; |
||
1596 | if (!_init) _sendSerialData(); |
||
1597 | } |
||
1598 | private void textBoxSerial1_TextChanged(object sender, EventArgs e) |
||
1599 | { |
||
1600 | serChanTitle[0] = textBoxSerial1.Text; |
||
1601 | } |
||
1602 | private void btnSer1_0_Click(object sender, EventArgs e) |
||
1603 | { |
||
1604 | trckbarSerial1.Value = 0; |
||
1605 | } |
||
1606 | |||
1607 | private void btnSer1_127_Click(object sender, EventArgs e) |
||
1608 | { |
||
1609 | trckbarSerial1.Value = 127; |
||
1610 | } |
||
1611 | |||
1612 | private void btnSer1_254_Click(object sender, EventArgs e) |
||
1613 | { |
||
1614 | trckbarSerial1.Value = 254; |
||
1615 | } |
||
1616 | |||
1617 | private void trckbarSerial1_ValueChanged(object sender, EventArgs e) |
||
1618 | { |
||
1619 | lblTbSerial1.Text = trckbarSerial1.Value.ToString(); |
||
1620 | serChan[0] = trckbarSerial1.Value; |
||
1621 | if (!_init) _sendSerialData(); |
||
1622 | } |
||
1623 | |||
1624 | private void textBoxSerial2_TextChanged(object sender, EventArgs e) |
||
1625 | { |
||
1626 | serChanTitle[2] = textBoxSerial2.Text; |
||
1627 | } |
||
1628 | |||
1629 | private void trckbarSerial2_ValueChanged(object sender, EventArgs e) |
||
1630 | { |
||
1631 | lblTbSerial2.Text = trckbarSerial2.Value.ToString(); |
||
1632 | serChan[1] = trckbarSerial2.Value; |
||
1633 | if (!_init) _sendSerialData(); |
||
1634 | |||
1635 | } |
||
1636 | |||
1637 | private void btnSer2_0_Click(object sender, EventArgs e) |
||
1638 | { |
||
1639 | trckbarSerial2.Value = 0; |
||
1640 | } |
||
1641 | |||
1642 | private void btnSer2_127_Click(object sender, EventArgs e) |
||
1643 | { |
||
1644 | trckbarSerial2.Value = 127; |
||
1645 | } |
||
1646 | |||
1647 | private void btnSer2_254_Click(object sender, EventArgs e) |
||
1648 | { |
||
1649 | trckbarSerial2.Value = 254; |
||
1650 | } |
||
2233 | - | 1651 | } |
1652 | public class IniFile |
||
1653 | { |
||
1654 | public string path; |
||
1655 | |||
1656 | [DllImport("kernel32")] |
||
1657 | private static extern long WritePrivateProfileString(string section, |
||
1658 | string key, string val, string filePath); |
||
1659 | |||
1660 | [DllImport("kernel32.dll", CharSet = CharSet.Auto)] |
||
1661 | static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, |
||
1662 | uint nSize, string lpFileName); |
||
1663 | |||
1664 | [DllImport("kernel32")] |
||
1665 | private static extern int GetPrivateProfileString(string section, |
||
1666 | string key, string def, StringBuilder retVal, |
||
1667 | int size, string filePath); |
||
1668 | |||
1669 | public IniFile(string INIPath) |
||
1670 | { |
||
1671 | path = INIPath; |
||
1672 | } |
||
1673 | |||
1674 | public void IniWriteValue(string Section, string Key, string Value) |
||
1675 | { |
||
1676 | WritePrivateProfileString(Section, Key, Value, this.path); |
||
1677 | } |
||
1678 | |||
1679 | public string IniReadValue(string Section, string Key) |
||
1680 | { |
||
1681 | StringBuilder temp = new StringBuilder(255); |
||
1682 | int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); |
||
1683 | return temp.ToString(); |
||
1684 | } |
||
1685 | //Ini_sections auslesen in String-Array |
||
1686 | public string[] IniSectionNames() |
||
1687 | { |
||
1688 | |||
1689 | // uint MAX_BUFFER = 32767; |
||
1690 | uint MAX_BUFFER = 8388608; |
||
1691 | IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER); |
||
1692 | uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path); |
||
1693 | if (bytesReturned == 0) |
||
1694 | { |
||
1695 | Marshal.FreeCoTaskMem(pReturnedString); |
||
1696 | return null; |
||
1697 | } |
||
1698 | string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString(); |
||
1699 | Marshal.FreeCoTaskMem(pReturnedString); |
||
1700 | //use of Substring below removes terminating null for split |
||
1701 | return local.Substring(0, local.Length - 1).Split('\0'); |
||
1702 | |||
1703 | |||
1704 | } |
||
1705 | } |
||
1706 | public static class ControlExtensions |
||
1707 | { |
||
1708 | /// <summary> |
||
1709 | /// Execute a threadsafe operation, when accessing a control via another thread |
||
1710 | /// action is a lamdaexpression |
||
1711 | /// e.g. comboBox1.ExecuteThreadSafe(() => comboBox1.Enabled = true); |
||
1712 | /// </summary> |
||
1713 | /// <param name="control"> The control </param> |
||
1714 | /// <param name="action"> The 'action' to perform </param> |
||
1715 | public static void ExecuteThreadSafe(this Control control, Action action) |
||
1716 | { |
||
1717 | if (control.InvokeRequired) |
||
1718 | { |
||
1719 | control.BeginInvoke(action); //"BeginInvoke" is an async call -> threadsafety error when called to many times successively -> then take "Invoke" |
||
1720 | } |
||
1721 | else |
||
1722 | { |
||
1723 | action.Invoke(); |
||
1724 | } |
||
1725 | } |
||
1726 | } |
||
1727 | |||
1728 | } |