diff --git a/Plane.FormationCreator/Formation/FlightTaskSingleCopterInfo_LED.cs b/Plane.FormationCreator/Formation/FlightTaskSingleCopterInfo_LED.cs index 7009aa4..25fb3ce 100644 --- a/Plane.FormationCreator/Formation/FlightTaskSingleCopterInfo_LED.cs +++ b/Plane.FormationCreator/Formation/FlightTaskSingleCopterInfo_LED.cs @@ -1,4 +1,5 @@ using GalaSoft.MvvmLight.Command; +using Plane.CommunicationManagement; using Plane.Copters; using Plane.Windows.Messages; using System; @@ -6,6 +7,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Windows.Input; @@ -13,6 +15,7 @@ namespace Plane.FormationCreator.Formation { public partial class FlightTaskSingleCopterInfo { + public static FlightTaskSingleCopterInfo CreateForFlyToTask(ICopter copter) { var info = new FlightTaskSingleCopterInfo(copter) @@ -57,6 +60,7 @@ namespace Plane.FormationCreator.Formation })); } } + private CommModuleManager _commModuleManager = CommModuleManager.Instance; private ICommand _SetAllCopterLEDCommand; public ICommand SetAllCopterLEDCommand @@ -81,8 +85,58 @@ namespace Plane.FormationCreator.Formation })); } } + bool LEDTestRun = false; + /* + private string _TestLEDtxt = "测试"; + public string TestLEDtxt + { + get { return _TestLEDtxt; } + set { Set(nameof(TestLEDtxt), ref _TestLEDtxt, value); } + } + LEDMode 0 = "常亮" + 1="同步闪烁(单色)" /> + 2 = "异步闪烁(随机)" /> + 3="渐亮" /> + 4 = "渐暗" /> + 5="呼吸灯" /> + 6 = "同步闪烁(随机)" /> + 7="异步闪烁(单色)" /> + 8 = "拉烟" />(飞控是50 ? ) + */ + //测试灯光 + private ICommand _TestCopterLEDCommand; + public ICommand TestCopterLEDCommand + { + get + { + return _TestCopterLEDCommand ?? (_TestCopterLEDCommand = new RelayCommand(async => + { + if (!LEDTestRun) + { + + Task.Run(() => + { + LEDTestRun = true; + foreach (LEDInfo vLEDInfo in LEDInfos) + { + Thread.Sleep((int)vLEDInfo.Delay * 1000); + if (!LEDTestRun) break; + _commModuleManager.LED_TaskAsync(vLEDInfo.LEDMode, vLEDInfo.LEDRate, vLEDInfo.LEDTimes, vLEDInfo.LEDRGB, this.Copter); + } + //LEDTestRun = false; + }); + }else + { + LEDTestRun = false; + _commModuleManager.LED_TaskAsync(99, 0, 0, "000000", this.Copter); + } + + })); + } + } + private ICommand _SetSelectedCopterLEDCommand; public ICommand SetSelectedCopterLEDCommand @@ -116,7 +170,7 @@ namespace Plane.FormationCreator.Formation { private double delay = 1; //灯光延时 单位:100ms (从当前任务开为0,延时几秒后设置LED) private int ledmode = 0; //灯光模式 0常亮 1闪烁 2随机闪烁(RGB无意义) - private int ledrate = 0; //闪烁延时 单位:100ms + private float ledRate = 0; //闪烁延时 单位:100ms private int ledtimes = 0; //次数 (预留、暂取消其意义) private string ledRGB = "FFFFFF"; //灯光颜色 @@ -129,7 +183,7 @@ namespace Plane.FormationCreator.Formation { delay = led.delay; ledmode = led.ledmode; - ledrate = led.ledrate; + ledRate = led.ledRate; ledRGB = led.ledRGB; } public double Delay @@ -143,10 +197,10 @@ namespace Plane.FormationCreator.Formation get { return ledmode; } set { ledmode = value; } } - public int LEDRate + public float LEDRate { - get { return ledrate; } - set { ledrate = value; } + get { return ledRate; } + set { ledRate = value; } } public int LEDTimes { diff --git a/Plane.FormationCreator/ViewModels/ConfigVirtualIdViewModel.cs b/Plane.FormationCreator/ViewModels/ConfigVirtualIdViewModel.cs index d1210f0..bfe406b 100644 --- a/Plane.FormationCreator/ViewModels/ConfigVirtualIdViewModel.cs +++ b/Plane.FormationCreator/ViewModels/ConfigVirtualIdViewModel.cs @@ -11,6 +11,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Input; using Microsoft.Practices.ServiceLocation; +using System.IO; namespace Plane.FormationCreator.ViewModels { @@ -52,6 +53,71 @@ namespace Plane.FormationCreator.ViewModels + /// + /// 清除所有编号 + /// + private ICommand _SaveVirtualIdCommand; + public ICommand SaveVirtualIdCommand + { + get + { + return _SaveVirtualIdCommand ?? (_SaveVirtualIdCommand = new RelayCommand(() => + { + string Savestr = ""; + foreach (var c in _copterManager.Copters) + { + Savestr += c.Id + "=" + c.VirtualId + ';'; + } + StreamWriter writer = new StreamWriter("vid.dat"); + writer.Write(Savestr); + writer.Close(); + Message.Show($"保存成功"); + })); + } + } + + + + + /// + /// 清除所有编号 + /// + private ICommand _LoadVirtualIdCommand; + public ICommand LoadVirtualIdCommand + { + get + { + return _LoadVirtualIdCommand ?? (_LoadVirtualIdCommand = new RelayCommand(() => + { + if (!File.Exists("vid.dat")) + return; + // string str = System.IO.File.ReadAllText(@"vid.dat"); + + string str = File.ReadAllText("vid.dat"); + + string[] arr = str.Split(';'); + foreach (string s in arr) + { + + string[] arrs = s.Split('='); + + + var copter = _copterManager.Copters.FirstOrDefault(c => c.Id == arrs[0]); + if (copter != null) + { + copter.VirtualId = int.Parse(arrs[1]); + } + } + Message.Show($"读入成功"); + })); + } + } + + + + + + /// /// 设置单个虚拟ID @@ -75,6 +141,7 @@ namespace Plane.FormationCreator.ViewModels copter.VirtualId = SingleVirtualId; Message.Show($"飞机{copter.Name} 设置编号={SingleVirtualId}"); } + })); } } diff --git a/Plane.FormationCreator/ViewModels/ControlPanelViewModel.cs b/Plane.FormationCreator/ViewModels/ControlPanelViewModel.cs index 15de030..dd9f77b 100644 --- a/Plane.FormationCreator/ViewModels/ControlPanelViewModel.cs +++ b/Plane.FormationCreator/ViewModels/ControlPanelViewModel.cs @@ -652,11 +652,55 @@ namespace Plane.FormationCreator.ViewModels foreach (string name in paramlist.Keys) { float value = float.Parse( paramlist[name].ToString()); - await Task.WhenAll(_copterManager.AcceptingControlCopters.Select - (copter => copter.SetParamAsync(name, value))); + //写5次 + for (int i = 0; i < 5; i++) + { + + if (_copterManager.AcceptingControlCopters.Count() < _copterManager.Copters.Count) + await _commModuleManager.SetParamAsync(name, value, _copterManager.AcceptingControlCopters); + else if (_copterManager.AcceptingControlCopters.Count() == _copterManager.Copters.Count) + { + _commModuleManager.SetAllCoptersForWifi(_copterManager.Copters); + await _commModuleManager.SetParamAsync(name, value); + } + + + + // await Task.WhenAll(_copterManager.AcceptingControlCopters.Select + // (copter => copter.SetParamAsync(name, value))); + } + await Task.Delay(2000); + + //发送读取5次 + for (int i = 0; i < 5; i++) + { + if (_copterManager.AcceptingControlCopters.Count() < _copterManager.Copters.Count) + await _commModuleManager.ReadParamAsnyc(name, _copterManager.AcceptingControlCopters); + else if (_copterManager.AcceptingControlCopters.Count() == _copterManager.Copters.Count) + await _commModuleManager.ReadParamAsnyc(name); + } + await Task.Delay(1000); + + //开始检查飞机返回参数是否正确 + + foreach (ICopter copter in _copterManager.AcceptingControlCopters) + { + if (value != copter.RetainInt) + { + ifallok = false; + Alert.Show(copter.Id + "参数[" + name + "]设置失败,读取的值是[" + copter.RetainInt + "]", "提示", MessageBoxButton.OK, MessageBoxImage.Information); + + } + + + } + + + + /* - await Task.WhenAll( + await Task.WhenAll( _copterManager.AcceptingControlCopters.Select(async copter => { @@ -669,7 +713,8 @@ namespace Plane.FormationCreator.ViewModels } )); - */ + */ + } @@ -702,14 +747,20 @@ namespace Plane.FormationCreator.ViewModels { float paramvalue = Convert.ToSingle(ModifyParamWindow.textParamValue.Text); int num = 0; - if (_copterManager.AcceptingControlCopters.Count() < _copterManager.Copters.Count) - num = await _commModuleManager.SetParamAsync(paramstr, paramvalue, _copterManager.AcceptingControlCopters); - else if (_copterManager.AcceptingControlCopters.Count() == _copterManager.Copters.Count) + int writeTime = 0; + //连续写5次,为了都写成功 + for ( writeTime=0; writeTime<5; writeTime++) { - _commModuleManager.SetAllCoptersForWifi(_copterManager.Copters); - num = await _commModuleManager.SetParamAsync(paramstr, paramvalue); + + if (_copterManager.AcceptingControlCopters.Count() < _copterManager.Copters.Count) + num = await _commModuleManager.SetParamAsync(paramstr, paramvalue, _copterManager.AcceptingControlCopters); + else if (_copterManager.AcceptingControlCopters.Count() == _copterManager.Copters.Count) + { + _commModuleManager.SetAllCoptersForWifi(_copterManager.Copters); + num = await _commModuleManager.SetParamAsync(paramstr, paramvalue); + } } - Alert.Show($"广播完成! 当前序列号:{num}"); + Alert.Show($"广播完成! 序列号从:{num-writeTime+1}到{num}"); } else { @@ -1462,6 +1513,7 @@ namespace Plane.FormationCreator.ViewModels { Color color = (Color)ColorConverter.ConvertFromString("#" + ledInfo.LEDRGB); int ledMode = ledInfo.LEDMode; + //拉烟任务模式ID是50,需要改为50 if (ledMode == 8) ledMode = 50; IMission LEDMission = Mission.CreateLEDControlMission( (int)(ledInfo.Delay * 10), diff --git a/Plane.FormationCreator/ViewModels/CopterListViewModel.cs b/Plane.FormationCreator/ViewModels/CopterListViewModel.cs index db2d193..ac73a58 100644 --- a/Plane.FormationCreator/ViewModels/CopterListViewModel.cs +++ b/Plane.FormationCreator/ViewModels/CopterListViewModel.cs @@ -21,6 +21,7 @@ using Newtonsoft.Json; using System.IO; using Newtonsoft.Json.Linq; using Plane.Windows.Messages; +using Plane.Collections; namespace Plane.FormationCreator.ViewModels { @@ -176,7 +177,8 @@ namespace Plane.FormationCreator.ViewModels copter.DisplayVirtualId = true; copter.DisplayID = false; } - + //强制刷新飞机显示 + // _copterManager.Copters.ForEach(copter => copter.RefreashLoc()); })); @@ -197,6 +199,8 @@ namespace Plane.FormationCreator.ViewModels copter.DisplayVirtualId = false; copter.DisplayID = true; } + //强制刷新飞机显示 + // _copterManager.Copters.ForEach(copter => copter.RefreashLoc()); })); } @@ -252,6 +256,8 @@ namespace Plane.FormationCreator.ViewModels copter.DisplayVirtualId = true; copter.DisplayID = true; } + //强制刷新飞机显示 + // _copterManager.Copters.ForEach(copter => copter.RefreashLoc()); })); } diff --git a/Plane.FormationCreator/ViewModels/View3DViewModel.cs b/Plane.FormationCreator/ViewModels/View3DViewModel.cs index 25696ed..6b2ac6c 100644 --- a/Plane.FormationCreator/ViewModels/View3DViewModel.cs +++ b/Plane.FormationCreator/ViewModels/View3DViewModel.cs @@ -200,13 +200,16 @@ namespace Plane.FormationCreator.ViewModels Color color; if (copter.LEDColor != null && copter.LEDColor != "") + { color = (Color)ColorConverter.ConvertFromString("#" + copter.LEDColor); - else - color = (Color)ColorConverter.ConvertFromString("#" + CopterManager.CopterDefaultColor); - panle3D.Material = MaterialHelper.CreateMaterial(color); - panle3D.BackMaterial = MaterialHelper.CreateMaterial(color); + panle3D.Material = MaterialHelper.CreateMaterial(color); + panle3D.BackMaterial = MaterialHelper.CreateMaterial(color); + } + //else + // color = (Color)ColorConverter.ConvertFromString("#" + CopterManager.CopterDefaultColor); - } + + } //Message.Show("添加3D飞机:" + copter.Name); } diff --git a/Plane.FormationCreator/Views/ConfigVirtualIdView.xaml b/Plane.FormationCreator/Views/ConfigVirtualIdView.xaml index 005ea53..d9c6178 100644 --- a/Plane.FormationCreator/Views/ConfigVirtualIdView.xaml +++ b/Plane.FormationCreator/Views/ConfigVirtualIdView.xaml @@ -29,9 +29,12 @@ - - -