using IModel; using Shaker.Models; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace ShakerService.ViewModel { internal sealed class ForceBalanceControlViewModel : ViewModelBase { /// /// 水平力反馈增益 /// public double HorizontalForceBalanceGain => CurrentModel.HorizontalForceBalanceGain; /// /// 竖直力反馈增益 /// public double VerticalForceBalanceGain =>CurrentModel.VerticalForceBalanceGain; /// /// 合成矩阵 /// public double[,] CompositionMatrix =>CurrentModel.CompositionMatrix; /// /// 自由度分解列矩阵 /// public double[,] FreedomDecompositionMatrix =>CurrentModel.FreedomDecompositionMatrix; private ForceBalanceControlViewModel() : base() { } static ForceBalanceControlViewModel() { } private protected override void UpModel(ForceBalanceControlModel model) { if(model.CompositionMatrix==null || model.CompositionMatrix.Length!=16) { model.CompositionMatrix = new double[2, 8]; } if(model.FreedomDecompositionMatrix ==null || model.FreedomDecompositionMatrix.Length!=16) { model.FreedomDecompositionMatrix = new double[8, 2]; } base.UpModel(model); } private protected override void ReadModel() { base.ReadModel(); Communication.Instance.DbConnection.CreateTable(); var model = Communication.Instance.DbConnection.Table().FirstOrDefault() ?? new ForceBalanceControlModel(); Communication.Instance.DbConnection.Execute($"CREATE TABLE IF NOT EXISTS {nameof(ForceBalanceControlModel.CompositionMatrix)} ({nameof(BaseModel.Id)} INTEGER PRIMARY KEY AUTOINCREMENT, Value REAL);"); var compositionMatrix = Communication.Instance.DbConnection.Query($"select Value from {nameof(ForceBalanceControlModel.CompositionMatrix)}")?.ToArray() ?? new double[16]; if(compositionMatrix==null || compositionMatrix.Length!=16) { compositionMatrix = new double[16]; } model.CompositionMatrix = new double[2, 8]; Unsafe.CopyBlock(ref Unsafe.As(ref model.CompositionMatrix[0, 0]), ref Unsafe.As(ref compositionMatrix[0]), (uint)(Unsafe.SizeOf() * compositionMatrix.Length)); Communication.Instance.DbConnection.Execute($"CREATE TABLE IF NOT EXISTS {nameof(ForceBalanceControlModel.FreedomDecompositionMatrix)} ({nameof(BaseModel.Id)} INTEGER PRIMARY KEY AUTOINCREMENT, Value REAL);"); var freedomDecompositionMatrix = Communication.Instance.DbConnection.Query($"select Value from {nameof(ForceBalanceControlModel.FreedomDecompositionMatrix)}")?.ToArray() ?? new double[16]; if (freedomDecompositionMatrix == null || freedomDecompositionMatrix.Length != 16) { freedomDecompositionMatrix = new double[16]; } model.FreedomDecompositionMatrix = new double[8,2]; Unsafe.CopyBlock(ref Unsafe.As(ref model.FreedomDecompositionMatrix[0, 0]), ref Unsafe.As(ref freedomDecompositionMatrix[0]), (uint)(Unsafe.SizeOf() * freedomDecompositionMatrix.Length)); CurrentModel = model; SaveModel(); } private protected override void SaveModel() { base.SaveModel(); Communication.Instance.DbConnection.DeleteAll(); Communication.Instance.DbConnection.Insert(CurrentModel); Communication.Instance.DbConnection.DeleteAll(tableName: nameof(ForceBalanceControlModel.CompositionMatrix)); for(int i=0;i(tableName: nameof(ForceBalanceControlModel.FreedomDecompositionMatrix)); for (int i = 0; i < CurrentModel.FreedomDecompositionMatrix.Length; i++) { Communication.Instance.DbConnection.Execute($"INSERT INTO {nameof(ForceBalanceControlModel.FreedomDecompositionMatrix)} (Value) VALUES ({Unsafe.Add(ref CurrentModel.FreedomDecompositionMatrix[0, 0], i)});"); } } public override void SetFpga() { base.SetFpga(); ShakerFpga.ShakerFpga.Instance.水平力反馈增益.Value = HorizontalForceBalanceGain; ShakerFpga.ShakerFpga.Instance.竖直力反馈增益.Value = VerticalForceBalanceGain; ShakerFpga.ShakerFpga.Instance.合成矩阵.Value = [.. CompositionMatrix]; ShakerFpga.ShakerFpga.Instance.自由度分解列矩阵.Value = [.. FreedomDecompositionMatrix]; } public static ForceBalanceControlViewModel Instance { get; } = new ForceBalanceControlViewModel(); } }