RotarySystem.cs 11.3 KB
Newer Older
1

2 3 4 5 6 7 8 9 10 11 12 13
using FLY.Thick.Blowing.RotaryMachine.IService;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

14
using WinderAccessory = FLY.DownBlowing.Common.WinderAccessory;
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
namespace FLY.DownBlowing.Server
{
    public class RotarySystem : IRotarySerivce
    {
        /// <summary>
        /// 方向相反
        /// </summary>
        public bool IsReversed { get; set; } = true;
        #region  状态
        /// <summary>
        /// 正方向运行 (逆时针旋转 counter-clockwise)
        /// </summary>
        public bool IsForw { get; set; }

        /// <summary>
        /// 反方向运行 (顺时针旋转 clockwise)
        /// </summary>
        public bool IsBackw { get; set; }

        /// <summary>
        /// 转向信号0 撞了它, 变为 正方向运行 
        /// </summary>
        public bool IsTurnSign0 { get; set; }

        /// <summary>
        /// 复位原点信号 (撞了它后, 证明在中间)
        /// </summary>
        public bool IsOrgSign { get; set; }

        /// <summary>
        /// 转向信号1 撞了它, 变为 反方向运行 
        /// </summary>
        public bool IsTurnSign1 { get; set; }

        /// <summary>
        /// 旋转塔电机频率设定(Hz)  就是速度 脉冲/秒
        /// </summary>
        public double RotaryFreqSet { get; set; }

        /// <summary>
        /// 旋转塔电机当前速度 脉冲/秒, +为正方向, -为反反向
        /// </summary>
        public double RSpeed { get; set; }

        /// <summary>
        /// 旋转塔顺时针换向信号发生时, 旋转架脉冲 (小值)
        /// </summary>
        public double RPosAtTurnSign0 { get; set; } = double.NaN;

        /// <summary>
        /// 旋转塔顺复位原点信号发生时, 旋转架脉冲 (中值)
        /// </summary>
        public double RPosAtOrgSign { get; set; } = double.NaN;

70 71 72 73 74
        /// <summary>
        /// 从换向信号1 到 原点信号, 灭到亮 发生时, 旋转架脉冲 (中值)
        /// </summary>
        public double RPosAtOrgSign1 { get; set; } = double.NaN;

75 76 77 78 79 80 81 82 83
        /// <summary>
        /// 旋转塔逆时针换向信号发生时, 旋转架脉冲(大值)
        /// </summary>
        public double RPosAtTurnSign1 { get; set; } = double.NaN;

        /// <summary>
        ///旋转架全局脉冲, 当前旋转架脉冲 = GlobalRPos - OrgRPos;
        /// </summary>
        public double RPosGlobal { get; set; } = 0;
84 85 86
        public double RPosOfR { get; set; } = 10000;

        public double Angle { get; set; }
87 88 89 90 91 92 93 94 95 96 97 98 99

        /// <summary>
        /// 旋转塔开启指示
        /// </summary>
        public bool IsRotaryOn { get; set; }

        /// <summary>
        /// 牵引速度显示(m/min)
        /// </summary>
        public double TractionVelocity { get; set; }
        #endregion

        public int TurnSign0Cnt { get; set; }
100 101 102 103

        public List<SignCell> Signs = new List<SignCell>();

        WinderAccessory winderAccessory;
104 105 106 107 108 109 110
        DispatcherTimer dispatcherTimer;
        Stopwatch stopwatch_rpos;

        public RotarySystem()
        {

        }
111
        public void Init(WinderAccessory _winderAccessory)
112 113 114 115 116 117 118 119 120 121 122 123 124
        {
            winderAccessory = _winderAccessory;

            if (!Load())
                Save();

            Misc.BindingOperations.SetBinding(winderAccessory, nameof(winderAccessory.T1Velocity), () =>
            {
                TractionVelocity = winderAccessory.T1Velocity;
            });

            Misc.BindingOperations.SetBinding(winderAccessory, nameof(winderAccessory.IsRotaryOn), this, nameof(IsRotaryOn));

125 126 127 128 129 130 131
            Misc.BindingOperations.SetBinding(winderAccessory,
                new string[]{
                nameof(winderAccessory.IsRotaryForw),
                nameof(winderAccessory.IsRotaryBackw),
                nameof(winderAccessory.IsRotaryForwTurn),
                nameof(winderAccessory.IsRotaryBackwTurn)},
                updateSign);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

            Misc.BindingOperations.SetBinding(winderAccessory, nameof(winderAccessory.IsRotaryOrgSign), this, nameof(IsOrgSign));


            Misc.BindingOperations.SetBinding(winderAccessory, nameof(winderAccessory.RotaryFreqSet), () =>
            {
                RotaryFreqSet = winderAccessory.RotaryFreqSet;
            });

            Misc.BindingOperations.SetBinding(winderAccessory, nameof(winderAccessory.RotaryFreq), () =>
            {
                updateRSpeed();
            });

            this.PropertyChanged += RotarySystem_PropertyChanged;

            dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromSeconds(0.1);
            dispatcherTimer.Tick += DispatcherTimer_Tick;
            stopwatch_rpos = new Stopwatch();
        }
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        void updateSign()
        {
            if (!IsReversed)
            {
                IsForw = winderAccessory.IsRotaryForw;
                IsBackw = winderAccessory.IsRotaryBackw;
                IsTurnSign0 = winderAccessory.IsRotaryForwTurn;
                IsTurnSign1 = winderAccessory.IsRotaryBackwTurn;
            }
            else
            {
                IsForw = winderAccessory.IsRotaryBackw;
                IsBackw = winderAccessory.IsRotaryForw;
                IsTurnSign0 = winderAccessory.IsRotaryBackwTurn;
                IsTurnSign1 = winderAccessory.IsRotaryForwTurn;
            }
        }
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        void updateRSpeed()
        {
            if (IsForw)
            {
                RSpeed = winderAccessory.RotaryFreq;
            }
            else if (IsBackw)
            {
                RSpeed = -winderAccessory.RotaryFreq;
            }
            else
            {
                RSpeed = 0;
            }
        }
        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            double totalSeconds = stopwatch_rpos.Elapsed.TotalSeconds;
            stopwatch_rpos.Restart();

            if (!IsBackw && !IsForw)
            {
                //没有运行
                return;
            }

            if (IsTurnSign0 || IsTurnSign1)
            {
                //转向信号触发时, 脉冲没有变化
                return;
            }
            RPosGlobal += RSpeed * totalSeconds;
202 203 204 205

            double p = RPosGlobal - RPosAtTurnSign0;

            Angle = 360 * (p - RPosOfR / 2) / RPosOfR;
206 207 208 209 210 211 212 213 214 215 216 217 218
        }

        public void StartMeasureRPos()
        {
            stopwatch_rpos.Restart();
            dispatcherTimer.Start();
        }
        public void StopMeasureRPos()
        {
            stopwatch_rpos.Stop();
            dispatcherTimer.Stop();
        }

219 220 221 222 223 224 225 226 227 228
        void AddSign(SignCell sign)
        {
            Signs.Insert(0, sign);
            while (Signs.Count > 100)
                Signs.RemoveAt(Signs.Count() - 1);
        }
        void AddSign(int no)
        {
            AddSign(new SignCell() { No = no, RPosGlobal = RPosGlobal, Time = DateTime.Now, RPos = RPosGlobal - RPosAtTurnSign0, RotaryFreqSet = RotaryFreqSet });
        }
229 230 231 232
        private void RotarySystem_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(IsReversed))
            {
233
                updateSign();
234 235 236 237 238 239
            }
            else if (e.PropertyName == nameof(IsTurnSign0))
            {
                if (IsTurnSign0)
                {
                    RPosAtTurnSign0 = RPosGlobal;
240
                    AddSign(0);
241 242 243 244 245 246
                    TurnSign0Cnt++;
                }
            }
            else if (e.PropertyName == nameof(IsTurnSign1))
            {
                if (IsTurnSign1)
247
                {
248
                    RPosAtTurnSign1 = RPosGlobal;
249 250 251
                    RPosOfR = RPosAtTurnSign1 - RPosAtTurnSign0;
                    AddSign(1);
                }
252 253 254
            }
            else if (e.PropertyName == nameof(IsOrgSign))
            {
255
                if (IsForw)
256
                {
257
                    if (IsOrgSign)
258 259
                    {
                        RPosAtOrgSign = RPosGlobal;
260 261 262 263 264 265
                        AddSign(20);
                    }
                    else
                    {
                        RPosAtOrgSign1 = RPosGlobal;
                        AddSign(21);
266 267
                    }
                }
268
                else if (IsBackw)
269
                {
270 271 272 273 274 275
                    if (IsOrgSign)
                    {
                        RPosAtOrgSign1 = RPosGlobal;
                        AddSign(21);
                    }
                    else if (!IsOrgSign)
276 277
                    {
                        RPosAtOrgSign = RPosGlobal;
278
                        AddSign(20);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
                    }
                }
            }
            else if (e.PropertyName == nameof(IsForw))
            {
                updateRSpeed();
            }
            else if (e.PropertyName == nameof(IsBackw))
            {
                updateRSpeed();
            }
        }

        string filePath = "rotary.json";

        public event PropertyChangedEventHandler PropertyChanged;

        public bool Save()
        {
            return RotarySystemJsonDb.Save(this, filePath);
        }
        bool Load()
        {
            return RotarySystemJsonDb.Load(this, filePath);
        }
    }
    public class RotarySystemJsonDb
    {


        /// <summary>
        /// 加载历史数据
        /// </summary>
        /// <returns></returns>
        public static bool Load(RotarySystem src, string file_path)
        {
            if (File.Exists(file_path))
            {
                try
                {
                    string json = File.ReadAllText(file_path);
                    var jsonDb = JsonConvert.DeserializeObject<RotarySystemJsonDb>(json);
                    src.IsReversed = jsonDb.IsReversed;
                    return true;
                }
                catch
                {
                    //异常,没有json 解码失败

                }

            }
            return false;
        }

        /// <summary>
        /// 保存
        /// </summary>
        public static bool Save(RotarySystem src, string file_path)
        {
            try
            {
                var jsonDb = new RotarySystemJsonDb() { IsReversed = src.IsReversed };
                string json = JsonConvert.SerializeObject(jsonDb, Formatting.Indented);
                File.WriteAllText(file_path, json);
                return true;
            }
            catch
            {
                //异常,没有json 编码失败

            }
            return false;
        }

        public bool IsReversed = true;
    }

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    public class SignCell : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 信号
        /// </summary>
        public int No { get; set; }

        /// <summary>
        /// 全局脉冲
        /// </summary>
        public double RPosGlobal { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        public DateTime Time { get; set; }

        /// <summary>
        /// 相对脉冲
        /// </summary>
        public double RPos { get; set; }

        /// <summary>
        /// 旋转频率设置Hz
        /// </summary>
        public double RotaryFreqSet { get; set; }
    }
386 387

}