From d81d8f30eaf22600510cba4e5a73067aef773597 Mon Sep 17 00:00:00 2001 From: pxzleo Date: Mon, 31 Jul 2017 01:05:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5RTK=E6=94=AF=E6=8C=81=20[Exte?= =?UTF-8?q?nsions.cs]=20=E4=BF=AE=E6=94=B9//=E5=88=B0=E8=BE=BE=E8=88=AA?= =?UTF-8?q?=E7=82=B9=E7=B2=BE=E5=BA=A60.6=E7=B1=B3////=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E7=A2=B0=E6=92=9E=E6=A3=80=E6=B5=8B=E8=B7=9D=E7=A6=BB2?= =?UTF-8?q?=E7=B1=B3=20=EF=BC=88=E9=A3=9E=E6=9C=BA=E9=A3=9E=E6=8E=A7?= =?UTF-8?q?=E4=B8=AD=E9=A3=9E=E8=A1=8C=E9=80=9F=E5=BA=A6=E6=94=B9=E4=B8=BA?= =?UTF-8?q?200/=E7=A7=92=EF=BC=89=20[ControlPanelViewModel.cs]=20=20?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E5=9F=BA=E7=AB=99=E6=95=B0=E6=8D=AE=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=20[ControlPanelView.xaml]=E5=8A=A0=E5=85=A5r?= =?UTF-8?q?tk=E5=8F=91=E9=80=81=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Formation/Extensions.cs | 6 +- .../ViewModels/ControlPanelViewModel.cs | 158 ++++++++++++++++++ .../Views/ControlPanelView.xaml | 13 ++ 3 files changed, 174 insertions(+), 3 deletions(-) diff --git a/Plane.FormationCreator/Formation/Extensions.cs b/Plane.FormationCreator/Formation/Extensions.cs index 53e8350..cda1a89 100644 --- a/Plane.FormationCreator/Formation/Extensions.cs +++ b/Plane.FormationCreator/Formation/Extensions.cs @@ -54,8 +54,8 @@ namespace Plane.FormationCreator.Formation public static bool ArrivedTarget(this ICopter copter, double targetLat, double targetLng, float targetAlt) { - return copter.DistanceTo(targetLat, targetLng, targetAlt) < 2; - // return copter.DistanceTo(targetLat, targetLng, targetAlt) < 1.5; + // return copter.DistanceTo(targetLat, targetLng, targetAlt) < 2; + return copter.DistanceTo(targetLat, targetLng, targetAlt) < 0.6; //到达航点精度 // return copter.DistanceTo(targetLat, targetLng, targetAlt) < 2; // added by ZJF } @@ -71,7 +71,7 @@ namespace Plane.FormationCreator.Formation public static bool IsTooCloseTo(this ICopter copter, ICopter copter2) { - return copter.DistanceTo(copter2) < 2; + return copter.DistanceTo(copter2) < 0.5; //最近距离2米 } } } diff --git a/Plane.FormationCreator/ViewModels/ControlPanelViewModel.cs b/Plane.FormationCreator/ViewModels/ControlPanelViewModel.cs index f5e9f36..7a4c4f5 100644 --- a/Plane.FormationCreator/ViewModels/ControlPanelViewModel.cs +++ b/Plane.FormationCreator/ViewModels/ControlPanelViewModel.cs @@ -14,11 +14,19 @@ using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; +using Plane.Communication; namespace Plane.FormationCreator.ViewModels { public class ControlPanelViewModel : ViewModelBase { + // serialport + internal static IConnection Rtkport ; + private static System.Threading.Thread thrtk; + private static bool trkthreadrun = false; + + + public ControlPanelViewModel(FormationController formationController, CopterManager copterManager) { _formationController = formationController; @@ -127,6 +135,156 @@ namespace Plane.FormationCreator.ViewModels })); } } + private string _RTKcomvalue = "COM3"; + public string RTKcomvalue + { + get { return _RTKcomvalue; } + set { Set(nameof(RTKcomvalue), ref _RTKcomvalue, value); } + } + + //RTK收发线程 + private static void mainloop() + { + + + + } + + + private async Task ReadRTKPacketAsync() + { + if (!Rtkport.IsOpen) + { + return null; + } + + // limit to 110 byte packets + byte[] buffer = new byte[180]; + int length = 0; + + try + { + if (Rtkport.BytesToRead() > 0)//while (Rtkport.BytesToRead() > 0) + + + { + int read = await Rtkport.ReadAsync(buffer, 0, Math.Min(buffer.Length, Rtkport.BytesToRead())); + + // sendData(buffer, (byte)read); + + } + + /* + if (await Rtkport.ReadAsync(buffer, 0, 1) == 0) + { + // 做简单数据解析,得到协议类型,后续数据长度等 + } + //根据数据长度读取实际数据 + if (await Rtkport.ReadAsync(buffer, 1, 1) == 0) + { + + } + */ + + } + catch (Exception ex) + { + return null; + } + return buffer; + + } + + + + private ICommand _SendRTKCommand; + public ICommand SendRTKCommand + { + get + { + return _SendRTKCommand ?? (_SendRTKCommand = new RelayCommand(async () => + { + Rtkport = new SerialPortConnection(RTKcomvalue,57600) as IConnection; + await Rtkport.OpenAsync(); + if (!Rtkport.IsOpen) + { + Alert.Show("无法打开" + RTKcomvalue , "警告", MessageBoxButton.OK, MessageBoxImage.Warning); + return; + } + trkthreadrun = true; //开始运行后台任务 + /* //线程方式后台运行rtk转发任务 + thrtk = new System.Threading.Thread(new System.Threading.ThreadStart(mainloop)) + { + IsBackground = true, + Name = "injectgps" + }; + thrtk.Start(); + */ + //后台任务方式运行rtk转发任务 + await Task.Run(async () => + { + //读取RTK数据循环 + while (trkthreadrun) + { + //读入RTK数据 + var packet = await ReadRTKPacketAsync().ConfigureAwait(false); + //分发到每个飞机 + foreach (var copter in _copterManager.Copters) + { + await copter.InjectGpsDataAsync(packet, (ushort)packet.Length); + } + + + await Task.Delay(200).ConfigureAwait(false); + + } + }).ConfigureAwait(false); + + /* + + await Task.Run(() => + { + if (!Rtkport.IsOpen) + { + return null; + } + + // var packet = await ReadRTKPacketAsync().ConfigureAwait(false); + + while (trkthreadrun) + { + + await ReadRTKPacketAsync(); + + // if (await Rtkport.ReadAsync(buffer, 0, Math.Min(buffer.Length, 110)) > 0) + { + + + + + + } + + + + + + + + + } + + + });*/ + + + })); + } + } + + + + private ICommand _LandCommand; public ICommand LandCommand diff --git a/Plane.FormationCreator/Views/ControlPanelView.xaml b/Plane.FormationCreator/Views/ControlPanelView.xaml index baa7ba2..705cc08 100644 --- a/Plane.FormationCreator/Views/ControlPanelView.xaml +++ b/Plane.FormationCreator/Views/ControlPanelView.xaml @@ -59,6 +59,19 @@ Text="{Binding AltP, UpdateSourceTrigger=PropertyChanged}" /> + + +