50,6 → 50,53 |
{ |
public partial class MainForm : Form |
{ |
String[] NC_Error = new string[44] |
{ |
"No Error", |
"FC not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A1_.22FC_not_compatible_.22", |
"MK3Mag not compatible" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A2_.22MK3Mag_not_compatible_.22", |
"no FC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A3_.22no_FC_communication_.22", |
"no compass communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A4_.22no_compass_communication_.22", |
"no GPS communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A5_.22no_GPS_communication_.22", |
"bad compass value" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A6_.22bad_compass_value.22", |
"RC Signal lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A7_.22RC_Signal_lost_.22", |
"FC spi rx error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A8_.22FC_spi_rx_error_.22", |
"ERR: no NC communication" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A9:_.22ERR:_no_NC_communication.22", |
"ERR: FC Nick Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A10_.22ERR:_FC_Nick_Gyro.22", |
"ERR: FC Roll Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A11_.22ERR:_FC_Roll_Gyro.22", |
"ERR: FC Yaw Gyro" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A12_.22ERR:_FC_Yaw_Gyro.22", |
"ERR: FC Nick ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A13_.22ERR:_FC_Nick_ACC.22", |
"ERR: FC Roll ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A14_.22ERR:_FC_Roll_ACC.22", |
"ERR: FC Z-ACC" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A15_.22ERR:_FC_Z-ACC.22", |
"ERR: Pressure sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A16_.22ERR:_Pressure_sensor.22", |
"ERR: FC I2C" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A17_.22ERR:_FC_I2C.22", |
"ERR: Bl Missing" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A18_.22ERR:_Bl_Missing.22", |
"Mixer Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A19_.22Mixer_Error.22", |
"FC: Carefree Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A20_.22FC:_Carefree_Error.22", |
"ERR: GPS lost" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A21_.22ERR:_GPS_lost.22", |
"ERR: Magnet Error" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A22_.22ERR:_Magnet_Error.22", |
"Motor restart" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A23_.22Motor_restart.22", |
"BL Limitation" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A24_.22BL_Limitation.22", |
"Waypoint range" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A25_.22Waypoint_range.22", |
"ERR:No SD-Card" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A26_.22ERR:No_SD-Card.22", |
"ERR:SD Logging aborted" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A27_.22ERR:SD_Logging_aborted.22", |
"ERR:Flying range!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A28_.22ERR:Flying_range.21.22", |
"ERR:Max Altitude" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A29_.22ERR:Max_Altitude.22", |
"No GPS Fix" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A30_.22No_GPS_Fix.22", |
"compass not calibrated" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A31_.22compass_not_calibrated.22", |
"ERR:BL selftest" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A32_.22ERR:BL_selftest.22", |
"no ext. compass" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A33_.22no_ext._compass.22", |
"compass sensor" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A34_.22compass_sensor.22", |
"FAILSAFE pos.!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A35_.22FAILSAFE_pos..21__.22", |
"ERR:Redundancy" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A36_.22ERR:Redundancy__.22", |
"Redundancy test" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A37_.22Redundancy_test_.22", |
"GPS Update rate" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A38_.22GPS_Update_rate.22", |
"ERR:Canbus" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A39_.22ERR:Canbus.22", |
"ERR: 5V RC-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A40_.22ERR:_5V_RC-Supply.22", |
"ERR:Power-Supply" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A41_.22ERR:Power-Supply.22", |
"ACC not calibr." + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A42_.22ACC_not_calibr..22", |
"ERR:Parachute!" + Environment.NewLine + "http://wiki.mikrokopter.de/ErrorCodes#A43_.22ERR:Parachute.21.22" |
}; |
|
[FlagsAttribute] |
enum NC_HWError0 : short |
130,6 → 177,10 |
/// datatable for the debug data array - displayed on settings tabpage in datagridview |
/// </summary> |
DataTable dtAnalog = new DataTable(); |
|
DataTable dtMotors1 = new DataTable(); |
DataTable dtMotors2 = new DataTable(); |
|
public MainForm() |
{ |
InitializeComponent(); |
137,6 → 188,21 |
dtAnalog.Columns.Add("ID"); |
dtAnalog.Columns.Add("Value"); |
dataGridView1.DataSource = dtAnalog; |
dtMotors1.Columns.Add("#"); |
dtMotors1.Columns.Add("Current"); |
dtMotors1.Columns.Add("Temp"); |
dtMotors2.Columns.Add("#"); |
dtMotors2.Columns.Add("Current"); |
dtMotors2.Columns.Add("Temp"); |
dgvMotors1.DataSource = dtMotors1; |
dgvMotors2.DataSource = dtMotors2; |
_initDTMotors(); |
dgvMotors1.Columns[0].Width = 24; |
dgvMotors1.Columns[1].Width = 74; |
dgvMotors1.Columns[2].Width = 74; |
dgvMotors2.Columns[0].Width = 24; |
dgvMotors2.Columns[1].Width = 74; |
dgvMotors2.Columns[2].Width = 74; |
simpleSerialPort.PortClosed += SimpleSerialPort_PortClosed; |
simpleSerialPort.PortOpened += SimpleSerialPort_PortOpened; |
simpleSerialPort.DataReceived += processMessage; |
155,6 → 221,7 |
{ |
_loadLabelNames(); |
_init = false; |
splitContainer1.SplitterDistance = 514; |
} |
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) |
{ |
185,7 → 252,7 |
{ |
if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); } |
|
if (_blctrlDataAutorefresh && _iCtrlAct == 2) { _readBLCtrl(true); Thread.Sleep(10); } |
if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); } |
|
if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); } |
check_HWError = true; |
209,7 → 276,6 |
if (!_bCBInit && cbOSD.SelectedIndex > -1) |
_OSDMenue(cbOSD.SelectedIndex); |
} |
|
private void chkbAutoDbg_CheckedChanged(object sender, EventArgs e) |
{ |
if(!_init) _debugDataAutorefresh = chkbAutoDbg.Checked; |
226,7 → 292,6 |
{ |
if (!_init) _OSDAutorefresh = chkbAutoOSD.Checked; |
} |
|
private void cbTimingDebug_SelectedIndexChanged(object sender, EventArgs e) |
{ |
if (cbTimingDebug.SelectedIndex > -1) |
317,7 → 382,10 |
|
#region functions |
|
/// <summary> Processing the messages and displaying them in the according form controls </summary> |
#region processing received data |
/// <summary> Processing the messages and displaying them in the according form controls |
/// function called by simpleSerialPort.DataReceived event |
/// </summary> |
/// <param name="message"> message bytearray recieved by SimpleSerialPort class </param> |
private void processMessage(byte[] message) |
{ |
340,7 → 408,7 |
if (adr == 255) { crcError++; } |
else crcError = 0; |
lblCRCErr.Invoke((Action)(() => lblCRCErr.Text = crcError.ToString())); |
|
//display the active controller (FC / NC) |
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...??? |
{ |
_iCtrlAct = adr; |
365,7 → 433,7 |
lblCtrl.Invoke((Action)(() => lblCtrl.Text = "....")); |
break; |
} |
_loadLabelNames(); |
_loadLabelNames(); |
} |
// else |
// Debug.Print("Address == 0?"); |
377,270 → 445,36 |
|
switch (cmdID) |
{ |
case 'A': |
if (iLableIndex < 32) |
{ |
sAnalogLabel[iLableIndex] = s; |
if (dtAnalog.Rows.Count < 32) |
dtAnalog.Rows.Add(s, ""); |
else |
dtAnalog.Rows[iLableIndex].SetField(0, s); |
|
_getAnalogLabels(iLableIndex + 1); |
} |
Debug.Print(s); |
|
case 'A': //Label names |
_processLabelNames(s); |
break; |
case 'D': |
if (data.Length == 66) |
{ |
int[] iAnalogData = new int[32]; |
|
int index = 0; |
Int16 i16 = 0; |
double dTemp = 0; |
for (int i = 2; i < 66; i += 2) |
{ |
i16 = data[i + 1]; |
i16 = (Int16)(i16 << 8); |
iAnalogData[index] = data[i] + i16; |
sAnalogData[index] = (data[i] + i16).ToString(); |
dtAnalog.Rows[index].SetField(1, sAnalogData[index]); |
|
if (adr == 2) //NC |
{ |
switch (index) |
{ |
case 0: //pitch (German: nick) |
artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10))); |
lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
break; |
case 1: //roll |
artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10))); |
lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
break; |
case 4: //altitude |
lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"))); |
break; |
case 7: //Voltage |
lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V"))); |
break; |
case 8: // Current |
lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"))); |
break; |
case 10: //heading |
lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°")); |
headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index]))); |
break; |
case 12: // SPI error |
lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index])); |
break; |
case 14: //i2c error |
lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index])); |
break; |
case 20: //Earthmagnet field |
lblNCMF.Invoke((Action)(() => lblNCMF.Text = sAnalogData[index] + "%")); |
break; |
case 21: //GroundSpeed |
lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = ((double)iAnalogData[index] / (double)100).ToString("0.00 m/s"))); |
break; |
case 28: //Distance East from saved home position -> calculate distance with distance N + height |
dTemp = Math.Pow((double)iAnalogData[index],2) + Math.Pow((double)iAnalogData[index - 1],2); |
dTemp = Math.Sqrt(dTemp)/ (double)10; //'flat' distance from HP with N/E |
// lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0.00"))); |
dTemp = Math.Pow(dTemp, 2) + Math.Pow(((double)iAnalogData[4] / (double)10), 2); //adding 'height' into calculation |
dTemp = Math.Sqrt(dTemp); |
lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0 m"))); |
break; |
case 31: //Sats used |
lblNCSat.Invoke((Action)(() => lblNCSat.Text = sAnalogData[index])); |
break; |
} |
} |
if (adr == 1) //FC |
{ |
switch (index) |
{ |
case 0: //pitch (German: nick) |
artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10))); |
lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
break; |
case 1: //roll |
artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10))); |
lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
break; |
case 5: //altitude |
lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"))); |
break; |
case 8: //heading |
lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°")); |
headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index]))); |
break; |
case 9: //Voltage |
lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V"))); |
break; |
case 10: //Receiver quality |
lblNCRC.Invoke((Action)(() => lblNCRC.Text = sAnalogData[index])); |
break; |
case 22: // Current |
lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"))); |
break; |
case 23: //capacity used |
lblNCCap.Invoke((Action)(() => lblNCCap.Text = (iAnalogData[index]).ToString("0 mAh"))); |
break; |
case 27: // SPI error |
lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index])); |
break; |
case 28: //i2c error |
lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index])); |
break; |
} |
} |
index++; |
} |
} |
else |
Debug.Print("wrong data-length (66): " + data.Length.ToString()); |
case 'D': //Debug data |
_processDebugVals(adr,data); |
break; |
|
case 'V': |
if (data.Length == 12) |
{ |
if (!check_HWError) |
{ |
string[] sVersionStruct = new string[10] { "SWMajor: ", "SWMinor: ", "ProtoMajor: ", "LabelTextCRC: ", "SWPatch: ", "HardwareError 1: ", "HardwareError 2: ", "HWMajor: ", "BL_Firmware: ", "Flags: " }; |
string sVersion = ""; |
//sbyte[] signed = Array.ConvertAll(data, b => unchecked((sbyte)b)); |
Log(LogMsgType.Warning, (adr == 1 ? "FC-" : "NC-") + "Version: "); |
sVersion = "HW V" + (data[7] / 10).ToString() + "." + (data[7] % 10).ToString(); |
Log(LogMsgType.Incoming, sVersion); |
sVersion = "SW V" + (data[0]).ToString() + "." + (data[1]).ToString() + ((char)(data[4] + 'a')).ToString(); |
Log(LogMsgType.Incoming, sVersion); |
Log(LogMsgType.Incoming, "BL-Firmware: V" + (data[8] / 100).ToString() + "." + (data[8] % 100).ToString()); |
} |
if(data[5] > 0) //error0 |
{ |
if(adr == 1) |
ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[5].ToString() + ": " + ((FC_HWError0)data[5]).ToString()); |
if (adr == 2) |
ErrorLog(LogMsgType.Error, "NC - HW-Error " + data[5].ToString() + ": " + ((NC_HWError0)data[5]).ToString()); |
} |
if (data[6] > 0) //error1 |
{ |
if (adr == 1) |
ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[6].ToString() + ": " + ((FC_HWError1)data[6]).ToString()); |
if (adr == 2) |
ErrorLog(LogMsgType.Error, "NC - Unknown HW-ERROR: " + data[6].ToString()); //@moment NC has only one error field |
} |
|
} |
check_HWError = false; |
case 'V': //Version |
_processVersion(adr, data); |
break; |
|
case 'K'://BL-CTRL debug data from NC |
if (data.Length == 6 && data[0] < 8) |
{ |
Label lbCur = new Label(), lbTemp = new Label(); |
switch (data[0]) |
{ |
case 0: |
lbCur = LBLNCM1Cur; |
lbTemp = LBLNCM1Temp; |
break; |
case 1: |
lbCur = LBLNCM2Cur; |
lbTemp = LBLNCM2Temp; |
break; |
case 2: |
lbCur = LBLNCM3Cur; |
lbTemp = LBLNCM3Temp; |
break; |
case 3: |
lbCur = LBLNCM4Cur; |
lbTemp = LBLNCM4Temp; |
break; |
case 4: |
lbCur = LBLNCM5Cur; |
lbTemp = LBLNCM5Temp; |
break; |
case 5: |
lbCur = LBLNCM6Cur; |
lbTemp = LBLNCM6Temp; |
break; |
case 6: |
lbCur = LBLNCM7Cur; |
lbTemp = LBLNCM7Temp; |
break; |
case 7: |
lbCur = LBLNCM8Cur; |
lbTemp = LBLNCM8Temp; |
break; |
} |
if (lbCur != null) |
lbCur.Invoke((Action)(() => lbCur.Text = ((double)data[1] / (double)10).ToString("0.0 A"))); |
if (lbTemp != null) |
lbTemp.Invoke((Action)(() => lbTemp.Text = data[2].ToString("0 °C"))); |
|
} |
|
case 'K'://BL-CTRL data |
_processBLCtrl(data); |
break; |
|
case 'O': //NC Data |
int i_16,iVal; |
i_16 = data[81]; |
i_16 = (Int16)(i_16 << 8); |
iVal = data[80] + i_16; |
lblNCCap.Invoke((Action)(() => lblNCCap.Text = iVal.ToString() + " mAh")); //Capacity used |
|
i_16 = data[56]; |
i_16 = (Int16)(i_16 << 8); |
iVal = data[55] + i_16; |
TimeSpan t = TimeSpan.FromSeconds(iVal); |
string Text = t.Hours.ToString("D2") + ":" + t.Minutes.ToString("D2") + ":" + t.Seconds.ToString("D2"); |
lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = Text.ToString())); //Flying time |
|
lblNCRC.Invoke((Action)(() => lblNCRC.Text = data[66].ToString())); //RC quality |
lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = data[69].ToString())); //NC Errornumber |
if (data[69] > 0) |
_readNCError(); |
_processNCData(data); |
break; |
|
case 'E': //NC error-string |
ErrorLog(LogMsgType.Error, "NC Error: " + s); |
|
break; |
|
case 'L': |
if(data.Length == 84) |
{ |
string sMessage = ""; |
iOSDPage = data[0]; |
iOSDMax = data[1]; |
if (cbOSD.Items.Count != iOSDMax) _initOSDCB(); |
sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 2, data.Length - 4)); |
OSD(LogMsgType.Incoming, sMessage.Substring(0,20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(20,20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(40,20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(60,20)); |
lblOSDPageNr.Invoke((Action)(()=>lblOSDPageNr.Text = iOSDPage.ToString("[0]"))); |
|
} |
else |
OSD(LogMsgType.Incoming,"Wrong length: " + data.Length + " (should be 84)"); |
case 'L': //OSD Menue (called by pagenumber) |
_processOSDSingle(data); |
break; |
case 'H': |
if(data.Length == 81) |
{ |
string sMessage = ""; |
sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1)); |
OSD(LogMsgType.Incoming, sMessage.Substring(0,20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(20,20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(40,20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(60,20)); |
|
} |
else |
OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)"); |
case 'H': //OSD Menue (with autoupdate - called by Key) |
_processOSDAuto(data); |
break; |
|
//default: |
657,7 → 491,332 |
} |
} |
} |
/// <summary> |
/// Analog label names 'A' |
/// each label name is returned as a single string |
/// and added to string array sAnalogLabel[] |
/// and the datatable dtAnalog |
/// </summary> |
/// <param name="s">the label name</param> |
void _processLabelNames(string s) |
{ |
if (iLableIndex < 32) |
{ |
sAnalogLabel[iLableIndex] = s; |
if (dtAnalog.Rows.Count < 32) |
dtAnalog.Rows.Add(s, ""); |
else |
dtAnalog.Rows[iLableIndex].SetField(0, s); |
|
_getAnalogLabels(iLableIndex + 1); |
} |
Debug.Print(s); |
} |
/// <summary> |
/// Debug values 'D' |
/// </summary> |
/// <param name="adr">adress of the active controller (1-FC, 2-NC)</param> |
/// <param name="data">the received byte array to process</param> |
void _processDebugVals(byte adr,byte[] data) |
{ |
if (data.Length == 66) |
{ |
int[] iAnalogData = new int[32]; |
|
int index = 0; |
Int16 i16 = 0; |
double dTemp = 0; |
for (int i = 2; i < 66; i += 2) |
{ |
i16 = data[i + 1]; |
i16 = (Int16)(i16 << 8); |
iAnalogData[index] = data[i] + i16; |
sAnalogData[index] = (data[i] + i16).ToString(); |
dtAnalog.Rows[index].SetField(1, sAnalogData[index]); |
|
if (adr == 2) //NC |
{ |
switch (index) |
{ |
case 0: //pitch (German: nick) |
artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10))); |
lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
break; |
case 1: //roll |
artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10))); |
lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
break; |
case 4: //altitude |
lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"))); |
break; |
case 7: //Voltage |
lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V"))); |
break; |
case 8: // Current |
lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"))); |
break; |
case 10: //heading |
lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°")); |
headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index]))); |
break; |
case 12: // SPI error |
lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index])); |
break; |
case 14: //i2c error |
lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index])); |
break; |
case 20: //Earthmagnet field |
lblNCMF.Invoke((Action)(() => lblNCMF.Text = sAnalogData[index] + "%")); |
break; |
case 21: //GroundSpeed |
lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = ((double)iAnalogData[index] / (double)100).ToString("0.00 m/s"))); |
break; |
case 28: //Distance East from saved home position -> calculate distance with distance N + height |
dTemp = Math.Pow((double)iAnalogData[index], 2) + Math.Pow((double)iAnalogData[index - 1], 2); |
dTemp = Math.Sqrt(dTemp) / (double)10; //'flat' distance from HP with N/E |
// lblNCDist.Invoke((Action)(() => lblNCDist.Text = dTemp.ToString("0.00"))); |
dTemp = Math.Pow(dTemp, 2) + Math.Pow(((double)iAnalogData[4] / (double)10), 2); //adding 'height' into calculation |
dTemp = Math.Sqrt(dTemp); |
lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = dTemp.ToString("0 m"))); |
break; |
case 31: //Sats used |
lblNCSat.Invoke((Action)(() => lblNCSat.Text = sAnalogData[index])); |
break; |
} |
} |
if (adr == 1) //FC |
{ |
switch (index) |
{ |
case 0: //pitch (German: nick) |
artificialHorizon1.Invoke((Action)(() => artificialHorizon1.pitch_angle = ((double)iAnalogData[index] / (double)10))); |
lblNCPitch.Invoke((Action)(() => lblNCPitch.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
break; |
case 1: //roll |
artificialHorizon1.Invoke((Action)(() => artificialHorizon1.roll_angle = ((double)iAnalogData[index] / (double)10))); |
lblNCRoll.Invoke((Action)(() => lblNCRoll.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0°"))); |
break; |
case 5: //altitude |
lblNCAlt.Invoke((Action)(() => lblNCAlt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 m"))); |
break; |
case 8: //heading |
lblNCCompass.Invoke((Action)(() => lblNCCompass.Text = sAnalogData[index] + "°")); |
headingIndicator1.Invoke((Action)(() => headingIndicator1.SetHeadingIndicatorParameters(iAnalogData[index]))); |
break; |
case 9: //Voltage |
lblNCVolt.Invoke((Action)(() => lblNCVolt.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 V"))); |
break; |
case 10: //Receiver quality |
lblNCRC.Invoke((Action)(() => lblNCRC.Text = sAnalogData[index])); |
break; |
case 22: // Current |
lblNCCur.Invoke((Action)(() => lblNCCur.Text = ((double)iAnalogData[index] / (double)10).ToString("0.0 A"))); |
break; |
case 23: //capacity used |
lblNCCap.Invoke((Action)(() => lblNCCap.Text = (iAnalogData[index]).ToString("0 mAh"))); |
break; |
case 27: // SPI error |
lblNCSPI.Invoke((Action)(() => lblNCSPI.Text = sAnalogData[index])); |
break; |
case 28: //i2c error |
lblNCI2C.Invoke((Action)(() => lblNCI2C.Text = sAnalogData[index])); |
break; |
} |
} |
index++; |
} |
} |
else |
Debug.Print("wrong data-length (66): " + data.Length.ToString()); |
} |
/// <summary> |
/// Version string 'V' |
/// </summary> |
/// <param name="adr">adress of the active controller (1-FC, 2-NC)</param> |
/// <param name="data">the received byte array to process</param> |
void _processVersion(byte adr,byte[] data) |
{ |
if (data.Length == 12) |
{ |
if (!check_HWError) |
{ |
string[] sVersionStruct = new string[10] { "SWMajor: ", "SWMinor: ", "ProtoMajor: ", "LabelTextCRC: ", "SWPatch: ", "HardwareError 1: ", "HardwareError 2: ", "HWMajor: ", "BL_Firmware: ", "Flags: " }; |
string sVersion = ""; |
//sbyte[] signed = Array.ConvertAll(data, b => unchecked((sbyte)b)); |
Log(LogMsgType.Warning, (adr == 1 ? "FC-" : "NC-") + "Version: "); |
sVersion = "HW V" + (data[7] / 10).ToString() + "." + (data[7] % 10).ToString(); |
Log(LogMsgType.Incoming, sVersion); |
sVersion = "SW V" + (data[0]).ToString() + "." + (data[1]).ToString() + ((char)(data[4] + 'a')).ToString(); |
Log(LogMsgType.Incoming, sVersion); |
Log(LogMsgType.Incoming, "BL-Firmware: V" + (data[8] / 100).ToString() + "." + (data[8] % 100).ToString()); |
} |
if (data[5] > 0) //error0 |
{ |
if (adr == 1) |
ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[5].ToString() + ": " + ((FC_HWError0)data[5]).ToString()); |
if (adr == 2) |
ErrorLog(LogMsgType.Error, "NC - HW-Error " + data[5].ToString() + ": " + ((NC_HWError0)data[5]).ToString()); |
} |
if (data[6] > 0) //error1 |
{ |
if (adr == 1) |
ErrorLog(LogMsgType.Error, "FC - HW-Error " + data[6].ToString() + ": " + ((FC_HWError1)data[6]).ToString()); |
if (adr == 2) |
ErrorLog(LogMsgType.Error, "NC - Unknown HW-ERROR: " + data[6].ToString()); //@moment NC has only one error field |
} |
|
} |
check_HWError = false; |
} |
/// <summary> |
/// BL-Ctrl data 'K' |
/// for FC you have to use a customized firmware |
/// </summary> |
/// <param name="data">the received byte array to process</param> |
void _processBLCtrl(byte[] data) |
{ |
if (data.Length % 6 == 0) //data.Length up to 96 (16 motors x 6 byte data) --> new datastruct in FC -> not standard! |
{ |
bool bAvailable = false; |
for (int i = 0; i < data.Length && data[i] < 8; i += 6) // data[i] < 8 --> at moment there are 8 display fields for motors |
{ |
|
if ((data[i + 4] & 128) == 128) //Status bit at pos 7 = 128 dec -- if true, motor is available |
bAvailable = true; |
else |
bAvailable = false; |
|
if (data[i] < 4) |
{ |
if (bAvailable) |
{ |
dtMotors1.Rows[data[i]].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A")); |
dtMotors1.Rows[data[i]].SetField(2, data[i + 2].ToString("0 °C")); |
} |
else |
{ |
dtMotors1.Rows[data[i]].SetField(1, "NA"); |
dtMotors1.Rows[data[i]].SetField(2, "NA"); |
} |
} |
if (data[i] > 3 && data[i] < 8) |
{ |
if (bAvailable) |
{ |
dtMotors2.Rows[data[i] - 4].SetField(1, ((double)data[i + 1] / (double)10).ToString("0.0 A")); |
dtMotors2.Rows[data[i] - 4].SetField(2, data[i + 2].ToString("0 °C")); |
} |
else |
{ |
dtMotors2.Rows[data[i] - 4].SetField(1, "NA"); |
dtMotors2.Rows[data[i] - 4].SetField(2, "NA"); |
} |
} |
} |
} |
|
} |
/// <summary> |
/// Navi-Ctrl data 'O' |
/// GPS-Position, capacatiy, flying time... |
/// </summary> |
/// <param name="data">the received byte array to process</param> |
void _processNCData(byte[] data) |
{ |
int i_32, i_16, iVal; |
double d; |
i_32 = data[4]; |
iVal = i_32 << 24; |
i_32 = data[3]; |
iVal += i_32 << 16; |
i_32 = data[2]; |
iVal += i_32 << 8; |
iVal += data[1]; |
d = (double)iVal / Math.Pow(10, 7); |
lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = d.ToString("0.######°"))); //GPS-Position: Longitude in decimal degree |
//lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = _convertDegree(d))); //GPS-Position: Longitude in minutes, seconds |
|
i_32 = data[8]; |
iVal = i_32 << 24; |
i_32 = data[7]; |
iVal += i_32 << 16; |
i_32 = data[6]; |
iVal += i_32 << 8; |
iVal += data[5]; |
d = (double)iVal / Math.Pow(10, 7); |
lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = d.ToString("0.######°"))); //GPS-Position: Latitude in decimal degree |
//lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = _convertDegree(d))); //GPS-Position: Latitude in minutes, seconds |
|
i_16 = data[81]; |
i_16 = (Int16)(i_16 << 8); |
iVal = data[80] + i_16; |
lblNCCap.Invoke((Action)(() => lblNCCap.Text = iVal.ToString() + " mAh")); //Capacity used |
|
i_16 = data[56]; |
i_16 = (Int16)(i_16 << 8); |
iVal = data[55] + i_16; |
TimeSpan t = TimeSpan.FromSeconds(iVal); |
string Text = t.Hours.ToString("D2") + ":" + t.Minutes.ToString("D2") + ":" + t.Seconds.ToString("D2"); |
lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = Text.ToString())); //Flying time |
|
lblNCRC.Invoke((Action)(() => lblNCRC.Text = data[66].ToString())); //RC quality |
lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = data[69].ToString())); //NC Errornumber |
//if (data[69] > 0) |
// _readNCError(); |
//break; |
if (data[69] > 0 & data[69] < 44) |
ErrorLog(LogMsgType.Error, "NC Error [" + data[69].ToString() + "]: " + NC_Error[data[69]]); |
|
} |
/// <summary> |
/// OSD Menue 'L' |
/// single page called by pagenumber |
/// no autoupdate |
/// </summary> |
/// <param name="data">the received byte array to process</param> |
void _processOSDSingle(byte[] data) |
{ |
if (data.Length == 84) |
{ |
string sMessage = ""; |
iOSDPage = data[0]; |
iOSDMax = data[1]; |
if (cbOSD.Items.Count != iOSDMax) _initOSDCB(); |
sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 2, data.Length - 4)); |
OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(60, 20)); |
lblOSDPageNr.Invoke((Action)(() => lblOSDPageNr.Text = iOSDPage.ToString("[0]"))); |
|
} |
else |
OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 84)"); |
|
} |
/// <summary> |
/// OSD Menue 'H' |
/// called by keys (0x01,0x02,0x03,0x04) |
/// autoupdate |
/// </summary> |
/// <param name="data">the received byte array to process</param> |
void _processOSDAuto(byte[] data) |
{ |
if (data.Length == 81) |
{ |
string sMessage = ""; |
sMessage = new string(ASCIIEncoding.ASCII.GetChars(data, 0, data.Length - 1)); |
OSD(LogMsgType.Incoming, sMessage.Substring(0, 20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(20, 20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(40, 20)); |
OSD(LogMsgType.Incoming, sMessage.Substring(60, 20)); |
|
} |
else |
OSD(LogMsgType.Incoming, "Wrong length: " + data.Length + " (should be 81)"); |
} |
#endregion processing received data |
|
/// <summary> send message to controller to request data |
/// for detailed info see http://wiki.mikrokopter.de/en/SerialProtocol/ |
/// </summary> |
913,11 → 1072,10 |
btnReadDebugCont.Invoke((Action)(() => btnReadDebugCont.BackColor = bReadContinously ? Color.FromArgb(192, 255, 192) : Color.FromArgb(224, 224, 224))); |
if (bReadContinously) |
{ |
_readDebugData(true); |
if (_iCtrlAct == 2) { Thread.Sleep(10); _readBLCtrl(true);} |
if (_iCtrlAct == 2) { Thread.Sleep(10); _readNavData(true);} |
Thread.Sleep(10); |
_OSDMenueAutoRefresh(); |
if (_debugDataAutorefresh) { _readDebugData(true); Thread.Sleep(10); } |
if (_blctrlDataAutorefresh) { _readBLCtrl(true); Thread.Sleep(10); } |
if (_navCtrlDataAutorefresh && _iCtrlAct == 2) { _readNavData(true); Thread.Sleep(10); } |
if (_OSDAutorefresh) { _OSDMenueAutoRefresh(); Thread.Sleep(10);} |
lblLifeCounter.Invoke((Action)(() => lblLifeCounter.BackColor = Color.FromArgb(0, 224, 0))); |
} |
else |
925,62 → 1083,20 |
_iLifeCounter = 0; |
} |
/// <summary> |
/// set fieldtexts to "NA" when not available with FC |
/// set values to "NA" when not available with FC |
/// </summary> |
void _setFieldsNA() |
{ |
Thread.Sleep(100); |
Label lbCur = new Label(), lbTemp = new Label(); |
for (int i = 0; i < 8; i++) |
{ |
//BL-Ctrl Temp & Current |
switch (i) |
{ |
case 0: |
lbCur = LBLNCM1Cur; |
lbTemp = LBLNCM1Temp; |
break; |
case 1: |
lbCur = LBLNCM2Cur; |
lbTemp = LBLNCM2Temp; |
break; |
case 2: |
lbCur = LBLNCM3Cur; |
lbTemp = LBLNCM3Temp; |
break; |
case 3: |
lbCur = LBLNCM4Cur; |
lbTemp = LBLNCM4Temp; |
break; |
case 4: |
lbCur = LBLNCM5Cur; |
lbTemp = LBLNCM5Temp; |
break; |
case 5: |
lbCur = LBLNCM6Cur; |
lbTemp = LBLNCM6Temp; |
break; |
case 6: |
lbCur = LBLNCM7Cur; |
lbTemp = LBLNCM7Temp; |
break; |
case 7: |
lbCur = LBLNCM8Cur; |
lbTemp = LBLNCM8Temp; |
break; |
} |
if (lbCur != null) |
lbCur.Invoke((Action)(() => lbCur.Text = "NA")); |
if (lbTemp != null) |
lbTemp.Invoke((Action)(() => lbTemp.Text = "NA")); |
|
} |
_initDTMotors(); |
lblNCFlTime.Invoke((Action)(() => lblNCFlTime.Text = "NA")); //FlightTime |
lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = "NA")); //NC ErrorNr |
lblNCMF.Invoke((Action)(() => lblNCMF.Text = "NA")); //earth magnet field |
lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = "NA")); //GroundSpeed |
lblNCDist.Invoke((Action)(() => lblNCDist.Text = "NA")); //Distance to HP |
lblNCSat.Invoke((Action)(() => lblNCSat.Text = "NA")); //Sats used |
lblNCErrNmbr.Invoke((Action)(() => lblNCErrNmbr.Text = "NA")); //NC ErrorNr |
lblNCMF.Invoke((Action)(() => lblNCMF.Text = "NA")); //earth magnet field |
lblNCGSpeed.Invoke((Action)(() => lblNCGSpeed.Text = "NA")); //GroundSpeed |
lblNCDistHP.Invoke((Action)(() => lblNCDistHP.Text = "NA")); //Distance to HP |
lblNCSat.Invoke((Action)(() => lblNCSat.Text = "NA")); //Sats used |
lblNCGPSLong.Invoke((Action)(() => lblNCGPSLong.Text = "NA")); //GPS position - longitude |
lblNCGPSLat.Invoke((Action)(() => lblNCGPSLat.Text = "NA")); //GPS position - latitude |
} |
/// <summary> |
/// one time query of the OSD Menue with pagenumber |
1077,6 → 1193,65 |
ini.IniWriteValue("default", "IntervalOSDData", OSDInterval.ToString()); |
} |
|
/// <summary> |
/// initialize the 2 datatables for motor values |
/// dtMotors1 - motor 1 - 4 |
/// dtMotors2 - motor 5 - 8 |
/// DataGridView dgvMotors1/2 are bound to dtMotors1/2 |
/// </summary> |
void _initDTMotors() |
{ |
for(int i = 0; i < 4; i++) |
{ |
if (dtMotors1.Rows.Count < 4) |
dtMotors1.Rows.Add((i + 1).ToString(), "NA", "NA"); |
else |
{ |
dtMotors1.Rows[i].SetField(1, "NA"); |
dtMotors1.Rows[i].SetField(2, "NA"); |
} |
if (dtMotors2.Rows.Count < 4) |
dtMotors2.Rows.Add((i + 5).ToString(), "NA", "NA"); |
else |
{ |
dtMotors2.Rows[i].SetField(1, "NA"); |
dtMotors2.Rows[i].SetField(2, "NA"); |
} |
} |
dgvMotors1.Invoke((Action)(() => dgvMotors1.Refresh())); |
dgvMotors2.Invoke((Action)(() => dgvMotors2.Refresh())); |
} |
|
/// <summary> |
/// Convert decimal degrees to degrees, minutes, seconds, milliseconds |
/// </summary> |
/// <param name="coord">the degree value as double</param> |
/// <returns>0° 0' 0,0"</returns> |
string _convertDegree(double coord) |
{ |
//double minutes = (degree - Math.Floor(degree)) * 60.0; |
//double seconds = (minutes - Math.Floor(minutes)) * 60.0; |
//double tenths = (seconds - Math.Floor(seconds)) * 10.0; |
//// get rid of fractional part |
//minutes = Math.Floor(minutes); |
//seconds = Math.Floor(seconds); |
//tenths = Math.Floor(tenths); |
|
|
//int sec = (int)Math.Round(coord * 3600); |
//int deg = sec / 3600; |
//sec = Math.Abs(sec % 3600); |
//int min = sec / 60; |
//sec %= 60; |
|
var ts = TimeSpan.FromHours(Math.Abs(coord)); |
double deg = Math.Sign(coord) * Math.Floor(ts.TotalHours); |
int min = ts.Minutes; |
int sec = ts.Seconds; |
int milli = ts.Milliseconds; |
|
return deg.ToString("0° ") + min.ToString("0") + "' " + sec.ToString("0") + "," + milli.ToString() + "\""; |
} |
#endregion functions |
|
#region buttons |
1199,7 → 1374,6 |
_OSDMenueAutoRefresh(4); |
} |
#endregion buttons |
|
} |
public class IniFile |
{ |