|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
错过了最佳的作品提交日期了。
前段时间帮同学做电子设计大赛,做的就是四旋翼的竞赛题目。感觉收货颇丰的。
在DIY作品中,还是做自己的老本行吧,所以就写了一套四旋翼的地面站来参赛了。
这个地面站经历了好几个版本的。
也为大家提供了通信协议的。方便大家能够使用 。
只要是通过串口,就可以实现数据的发送和显示。
附带了 通信协议的
消息格式:*高度,pitch,roll,yaw,经度,纬度,rc1,rc2,rc3,rc4;
除经纬度之外,其他必须为整型,有bug还在测试中。roll角度为0-360°
消息:*1522,15,12,12,2.33666555,3.22555554,1500,1500,1500,1500;
为了方便调试,这里也做了一些修改的。
看了到调试界面了吗?
这个是演示视频的。看看效果的
为了让更多的人了解一下这个东西,附上串口代码的:
void Serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int n =SP.BytesToRead;
byte[] buf = new byte[n+1];
(sender as SerialPort).Read(buf, 0, n);
//Array.Resize(ref buf, n);
// MessageBox.Show( String.Format("{0}{1}", BitConverter.ToString(buf), Environment.NewLine));
//MessageBox.Show(Encoding.Default.GetString(buf));
int m=0;
while (m != n)
{
if (buf[m] == '*' )
{
msg_num = 0;
is_exit1 = 1;
}
else if (buf[m] == '#')
{
msg_num = 0;
is_exit1 = 2;
}
if(buf[m] == ';')
{
//msg_num = 0;
if (is_exit1 == 1)
{
is_exit2 = 1;
pcmsg[msg_num] = buf[m];
}
else
{
msg_num = 0;
is_exit1 = 0;
is_exit2 = 0;
pcmsg = null;
pcmsg = new byte[400];
return;
}
break;
}
pcmsg[msg_num]=buf[m];
m++;
msg_num++;
if (msg_num >= 399)
{
msg_num = 0;
is_exit1 = 0;
is_exit2 = 0;
pcmsg = null;
pcmsg = new byte[400];
return;
}
}
if (is_exit2 == 1 && is_exit1 == 2)
{
string recvstr = Encoding.Default.GetString(pcmsg);
RECV.Text = recvstr;
string mstr = recvstr.Substring(1, msg_num - 1);
msg.Text = mstr;
string str2 = mstr.Replace(",", "");
int strlen = mstr.Length - str2.Length + 1;
string[] s = mstr.Split(',');
if (strlen != 9)
{
is_exit2 = 0;
is_exit1 = 0;
pcmsg = null;
pcmsg = new byte[400];
return;
}
PP.Text =(( Convert.ToDouble(s[0]))/100).ToString();
PI.Text = ((Convert.ToDouble(s[1])) / 100).ToString();
PD.Text = ((Convert.ToDouble(s[2])) / 100).ToString();
YP.Text = ((Convert.ToDouble(s[3])) / 100).ToString();
YI.Text = ((Convert.ToDouble(s[4])) / 100).ToString();
YD.Text = ((Convert.ToDouble(s[5])) / 100).ToString();
AP.Text = ((Convert.ToDouble(s[6])) / 100).ToString();
AI.Text = ((Convert.ToDouble(s[7])) / 100).ToString();
AD.Text = ((Convert.ToDouble(s[8])) / 100).ToString();
msg_num = 0;
is_exit1 = 0;
is_exit2 = 0;
}
if (is_exit2 == 1 && is_exit1==1)
{
string recvstr=Encoding.Default.GetString(pcmsg);
RECV.Text = recvstr;
string mstr =recvstr.Substring(1, msg_num-1);
msg.Text = mstr;
string str2= mstr.Replace(",","");
int strlen =mstr.Length- str2.Length+1;
string[] s = mstr.Split(',');
// MessageBox.Show(strlen.ToString());
if (strlen >= 10)
{
val.Text = s[0] + Environment.NewLine + s[1] + Environment.NewLine + s[2] + Environment.NewLine + s[3] + Environment.NewLine + s[4]
+ Environment.NewLine + s[5] + Environment.NewLine + s[6] + Environment.NewLine + s[7] + Environment.NewLine + s[8]
+ Environment.NewLine + s[9];
}
if (strlen >= 21 || strlen < 19)
{
is_exit2 = 0;
is_exit1 = 0;
pcmsg = null;
pcmsg = new byte[400];
return;
}
//娑堟伅鏍煎紡锛?楂樺害锛宲itch锛宺oll锛寉aw锛岀粡搴︼紝绾害锛宺c1锛宺c2锛宺c3锛宺c4锛?
if(strlen >0)
if (reg.IsMatch(s[0]) && s[0] != ""&& s[0].Length <6 )
{
altimeterIndicator1.SetAlimeterParameters(Convert.ToInt16(s[0])%65535);
}
if (strlen > 2)
if (reg.IsMatch(s[1]) && reg.IsMatch(s[2]) && s[1] != ""
&& s[2] != "" && s[1].Length< 4 && s[2].Length<4)
{
mpitch =-Convert.ToInt16(s[1]);
mroll =-Convert.ToInt16(s[2]);
attitudeIndicator1.SetAttitudeIndicatorParameters(-mpitch, mroll);
}
if (strlen > 3)
if (reg.IsMatch(s[3]) && s[3] != "" && s[3].Length < 5)
{
int tyaw=0;
tyaw =Convert.ToInt16(s[3]);;
tyaw = 180 - tyaw;
myaw = tyaw;
headingIndicator1.SetHeadingIndicatorParameters(tyaw);
}
if (strlen >2 )
if (reg.IsMatch(s[0]) && reg.IsMatch(s[1]) && reg.IsMatch(s[2])
&& s[0] != "" && s[1] != "" && s[2] != "" )
{
bFI1.Altitude = Convert.ToInt16(s[0]);
bFI1.Pitch = -Convert.ToInt16(s[1]);
bFI1.Bank = mroll;//Convert.ToInt16(s[2]);
bFI2.Altitude = Convert.ToInt16(s[0]);
bFI2.Pitch =- Convert.ToInt16(s[1]);
bFI2.Bank = mroll;//Convert.ToInt16(s[2]);
}
if (strlen > 4)
{
wd.Text = s[4];
dwd.Text = s[4];
}
if (strlen > 5)
{
jd.Text = s[5];
djd.Text = s[5];
}
if (strlen > 6)
if (reg.IsMatch(s[6]) && s[6] != "" && s[6].Length < 5)
{
rc1.Value = limit(Convert.ToInt16(s[6]));
rc1l.Text = s[6];
}
if (strlen >7 )
if (reg.IsMatch(s[7]) && s[7] != "" && s[7].Length < 5)
{
rc2.Value = limit(Convert.ToInt16(s[7]));
rc2l.Text = s[7];
}
if (strlen > 8)
if (reg.IsMatch(s[8]) && s[8] != "" && s[8].Length < 5)
{
rc3.Value = limit(Convert.ToInt16(s[8]));
rc3l.Text = s[8];
}
if (strlen >9 )
if (reg.IsMatch(s[9]) && s[9] != "" && s[9].Length < 5)
{
rc4.Value = limit(Convert.ToInt16(s[9]));
rc4l.Text = s[9];
}
if (strlen > 10)
if (reg.IsMatch(s[10]) && s[10] != "" && s[10].Length < 5)
{
rc5.Value = limit(Convert.ToInt16(s[10]));
rc5l.Text = s[10];
}
if (strlen > 11)
if (reg.IsMatch(s[11]) && s[11] != "" && s[11].Length < 5)
{
rc6.Value = limit(Convert.ToInt16(s[11]));
rc6l.Text = s[11];
}
if (strlen > 12)
if (reg.IsMatch(s[12]) && s[12] != "" && s[12].Length < 5)
{
rc7.Value = limit(Convert.ToInt16(s[12]));
rc7l.Text = s[12];
}
if (strlen > 13)
if (reg.IsMatch(s[13]) && s[13] != "" && s[13].Length < 5)
{
rc8.Value = limit(Convert.ToInt16(s[13]));
rc8l.Text = s[13];
}
if (strlen > 14)
pitchtext.Text = s[14];
if (strlen > 15)
rolltext.Text = s[15];
if (strlen > 16)
yawtext.Text = s[16];
if (strlen > 17)
alttext.Text = s[17];
if (strlen > 18)
mjd.Text = s[18];
if (strlen > 19)
mwd.Text = s[19];
is_exit2 = 0;
is_exit1 = 0;
pcmsg = null;
pcmsg = new byte[400];
}
}
|
| |