d23004
2023-05-24 fa87a558389d9d425f70afa1c8de54c69875ef2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
using SA_LTT.Base;
using System;
using System.Threading;
 
namespace SA_LTT.Module
{
    public class Attenuator : ComPort
    {
        public enum MotorRunState
        {
            Stopped,
            Accelerating,
            Decelerating,
            Running,
        }
 
        public enum MicroSteppingResolution : int
        {
            /// <summary>
            /// 1 Motor is driven in full steps mode.Waveplate holder turns once in 15600 steps.
            /// </summary>
            FullStep = 1,
            /// <summary>
            /// 2 Half step mode.Waveplate holder turns once in 31200 steps.
            /// </summary>
            HalfStep = 2 ,
            /// <summary>
            /// 4 Quarter step mode.Waveplate holder turns once in 62400 steps.
            /// </summary>
            QuaterStep = 4,
            /// <summary>
            /// 8 Eight step mode.Waveplate holder turns once in 124800 steps.
            /// </summary>
            EightStep = 8,
            /// <summary>
            /// 16 Sixteen step mode.Waveplate holder turns once in 249600 steps.
            /// </summary>
            SixteenStep = 16,
        }
 
        private MotorRunState _motorState;
        private int _position;
        private int _speed;
        private int _accelerationValue;
        private int _decelerationValue;
        private MicroSteppingResolution _steppingResolution;
        private bool _motorEnable;
 
        private Equipment _equipment;
        private Thread t_statusUpdate;
 
        public double DegreePerSecondFromSpeed
        {
            get
            {
                return 14400000 / (78 * (int)SteppingResolution * (65535 - Speed));
            }
        }
 
        public int Position
        {
            get
            {
                return _position;
            }
 
            private set
            {
                _position = value;
            }
        }
 
        public double Degree
        {
            get
            {
                return Math.Round(Position * GetDegreefromSteppingResolution(), 3);
            }
        }
 
        public int Speed
        {
            get
            {
                return _speed;
            }
 
            private set
            {
                _speed = value;
            }
        }
 
        public int AccelerationValue
        {
            get
            {
                return _accelerationValue;
            }
 
            private set
            {
                _accelerationValue = value;
            }
        }
 
        public int DecelerationValue
        {
            get
            {
                return _decelerationValue;
            }
 
            private set
            {
                _decelerationValue = value;
            }
        }
 
        public MicroSteppingResolution SteppingResolution
        {
            get
            {
                return _steppingResolution;
            }
 
            private set
            {
                _steppingResolution = value;
            }
        }
 
        public bool MotorEnable
        {
            get
            {
                return _motorEnable;
            }
 
            private set
            {
                _motorEnable = value;
            }
        }
 
        public MotorRunState MotorState
        {
            get
            {
                return _motorState;
            }
 
            private set
            {
                _motorState = value;
            }
        }
 
        public Attenuator(Equipment equipment)
        {
            serialPort.PortName = "COM11";
            serialPort.BaudRate = 38400;
            serialPort.Parity = System.IO.Ports.Parity.None;
            serialPort.DataBits = 8;
            serialPort.StopBits = System.IO.Ports.StopBits.One;
            serialPort.ReadTimeout = 500;
            ReceiveWaitSeconds = 0.5;
            Terminator = "\n\r";
 
            _steppingResolution = MicroSteppingResolution.FullStep;
 
            _equipment = equipment;
 
            t_statusUpdate = new Thread(statusUpdate);
            t_statusUpdate.Start();
        }
 
        public void statusUpdate()
        {
            while (_equipment.IsDisposed == false)
            {
                try
                {
                    Thread.Sleep(50);
 
                    if (IsOpen)
                    {
                        ReadSettings();
                        ReadStatus();
                    }
                    else
                    {
                        if (_equipment.alarmManager.OccurredAlarms.Exists(x => x.Code == AlarmCode.AL_0053_ATTENUATOR_DISCONNECTED))
                        {
 
                        }
                        else
                        {
                            if (Open() == false)
                            {
                                _equipment.alarmManager.Occur(AlarmCode.AL_0053_ATTENUATOR_DISCONNECTED);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace);
                }
            }
        }
 
        public void Home()
        {
            Write("zp");
        }
        
        public void Stop()
        {
            Write("st");
        }
        
        public void MoveAbsolute(float angle)
        {
            int position = (int)(angle / GetDegreefromSteppingResolution());
 
            Write($"g {position}");
        }
 
        public void MoveAbsolute(int position)
        {
            Write($"g {position}");
        }
 
        public void MoveRelative(double angle)
        {
            int position = (int)(angle / GetDegreefromSteppingResolution());
 
            Write($"m {position}");
        }
 
        public void MoveRelative(int position)
        {
            Write($"m {position}");
        }
 
        public void JogPositive()
        {
            Write($"m 2147483646");
        }
 
        public void JogNegative()
        {
            Write($"m -2147483646");
        }
 
        public bool IsAttenuatorAngleCorrect(float angle)
        {
            if (Position == (int)(angle / GetDegreefromSteppingResolution()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        public void SetMicroSteppingResolution(MicroSteppingResolution steppingResolution)
        {
            if (steppingResolution == MicroSteppingResolution.SixteenStep)
            {
                Write($"r 6");
            }
            else
            {
                Write($"r {(int)steppingResolution}");
            }
        }
        
        public double GetDegreefromSteppingResolution()
        {
            double degree = 360.0 / (78 * (int)SteppingResolution * 200);
            
            return degree;
        }
 
        public double GetDegreePerSecondFromSpeed(int speed)
        {
            double degreePerSecond = 14400000 / (78 * (int)SteppingResolution * (65535 - speed));
 
            return degreePerSecond;
        }
 
        public int GetSpeedFromDegreePerSecond(double degreePerSecond)
        {
            int speed = (int)Math.Ceiling(65535 - 14400000 / (degreePerSecond * 78 * (int)SteppingResolution));
 
            return speed;
        }
 
        /// <summary>
        /// 0 is turn off.
        /// </summary>
        /// <param name="acceleration"></param>
        public void SetAcceleration(int acceleration)
        {
            if(acceleration > 255)
            {
                acceleration = 255;
            }
            else if (acceleration < 0)
            {
                acceleration = 0;
            }
 
            Write($"a {acceleration}");
        }
 
        /// <summary>
        /// 0 is turn off
        /// </summary>
        /// <param name="deceleration"></param>
        public void SetDeceleration(int deceleration)
        {
            if (deceleration > 255)
            {
                deceleration = 255;
            }
            else if (deceleration < 0)
            {
                deceleration = 0;
            }
 
            Write($"d {deceleration}");
        }
 
        public void SetSpeed(int speed)
        {
            if (speed > 65000)
            {
                speed = 65000;
            }
            else if (speed < 1)
            {
                speed = 1;
            }
 
            Write($"s {speed}");
        }
 
        public void SaveSetting()
        {
            Write("ss");
        }
 
        public void ReadStatus()
        {
            string receivedData;
 
            if (WriteRead("o", out receivedData))
            {
                string[] status = receivedData.Split(';');
 
                if (status.Length < 2) return;
 
                MotorState = (MotorRunState)Enum.Parse(typeof(MotorRunState), status[0]);
                int position;
 
                int.TryParse(status[1], out position);
 
                this.Position = position;
            }
            else
            {
 
            }
        }
 
        public void ReadSettings()
        {
            try
            {
                string receivedData;
 
                if (WriteRead("pc", out receivedData))
                {
                    string[] settings = receivedData.Split(';');
 
                    if (settings.Length < 25) return;
 
                    bool operatingMode = settings[0] == "1" ? true : false;
                    MotorState = (MotorRunState)Enum.Parse(typeof(MotorRunState), settings[1]);
                    AccelerationValue = int.Parse(settings[2]);
                    DecelerationValue = int.Parse(settings[3]);
                    Speed = int.Parse(settings[4]);
                    int motionCurrentValue = int.Parse(settings[5]);
                    int idleCurrentValue = int.Parse(settings[6]);
                    int stepDirCurrentvalue = int.Parse(settings[7]);
 
                    if (settings[8] == "6")
                    {
                        SteppingResolution = MicroSteppingResolution.SixteenStep;
                    }
                    else
                    {
                        SteppingResolution = (MicroSteppingResolution)Enum.Parse(typeof(MicroSteppingResolution), settings[8]);
                    }
 
                    MotorEnable = settings[9] == "1" ? true : false;
                    
                    // settings[11] 필요 없을듯.
                    // settings[12] 필요 없을듯. Zero position 올 때마다 zp:[integerValue] 리턴해 주는거. 굳이 ..?
                    // settings[13] Reserved
                    // settings[14] Reserved
                    // settings[15] Reserved
                    // settings[16] 필요 없을듯.
 
                    // settings[21] Reserved
                    // settings[22] Reserved
                    // settings[23] Reserved
                }
            }
            catch(Exception e)
            {
                EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace);
            }
        }
        
        public new bool Write(string command)
        {
            lock (this)
            {
                //base.Write(command + '\r');
 
                //return true;
 
                Terminator = null;
                string data = base.WriteRead(command + '\r');
 
                if (data.StartsWith(command.Split(' ')[0]))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
 
        public bool WriteRead(string command, out string receivedData)
        {
            lock (this)
            {
                Terminator = "\n\r";
                string data = base.WriteRead(command + '\r');
 
                if (data.StartsWith(command.Split(' ')[0]))
                {
                    receivedData = data.Remove(0, command.Split(' ')[0].Length).Replace("\n\r", "");
                    return true;
                }
                else
                {
                    receivedData = string.Empty;
                    return false;
                }
            }
        }
    }
}